Android:sqliteopenhelper and Greendao Database operations

Source: Internet
Author: User

Original address: Http://blog.csdn.net/intbird

The project is using Greedao,sqllite, which was previously practiced,

If the project all use this, people will be more tired;

However, Greendao is easy to use and can be said to be a three-minute crash.

If you do not understand, please see the official documents, the source code is also described in detail.

===================
@GreenDao

Website Link:http://greendao-orm.com/documentation/how-to-get-started/

Website Demo: Https://github.com/greenrobot/greenDAO

public static void Save (Locations locs) {sqlitedatabase db = Frame.getinstance (). Getdbhelp (). Getwritabledatabase ();D Aomaster master = new Daomaster (db);D aosession session = master.newsession ();D Blocationsdao Locsdao = Session.getdblocationsdao (); Locsdao.insertorreplace (locs);;d b.close ();} public static void Deletedfromlast (Long LastID) {Sqlitedatabase db = Frame.getinstance (). Getdbhelp (). Getwritabledatabase ();D aomaster master = new Daomaster (db);D aosession session = master.newsession ();D Blocationsdao Locsdao = Session.getdblocationsdao (); querybuilder<dblocations> query = Locsdao.querybuilder ();D eletequery<dblocations> delete = Query.where ( Properties.Time.le (LastID)). Builddelete ();d elete.executedeletewithoutdetachingentities ();d b.close (); public static long Getdbsize () {Sqlitedatabase db = Frame.getinstance (). Getdbhelp (). Getwritabledatabase ();D Aomaster master = new Daomaster (db);D aosession session = master.newsession ();D Blocationsdao Locsdao = Session.getdblocationsdao ( ); longSize = Locsdao.querybuilder (). Count (); return size;} public static list<dblocations> getdatabysize (int size) {Sqlitedatabase db = Frame.getinstance (). Getdbhelp (). Getwritabledatabase ();D aomaster master = new Daomaster (db);D aosession session = master.newsession ();D Blocationsdao Locsdao = Session.getdblocationsdao (); list<dblocations> list = Locsdao.querybuilder (). limit (size). ORDERASC (Properties.time). Build (). List (); Db.close (); return list;}

============

@SqlLiteOpenHelper

Example

Package Mode;import Java.util.arraylist;import Java.util.hashmap;import java.util.map;import Core.override.myselfdishadapter;import Android.content.contentvalues;import Android.content.context;import Android.database.cursor;import android.database.sqlite.*;p ublic class Dishsqlite extends Sqliteopenhelper {private Static final String tb_name = "Tb_dishmode";p rivate static final String create_table_sql = "CREATE TABLE" +tb_name+ "(" + Di shmode.ds_id+ "Integer primary key AutoIncrement," + dishmode.ds_name+ "text," + dishmode.ds_cover+ "text," + Dishmode.ds_ img+ "text," + dishmode.ds_tips+ "text," + dishmode.ds_addtime+ "text)";p ublic dishsqlite (context context) {Super (conte XT, Tb_name, NULL, 1);} @Overridepublic void OnCreate (Sqlitedatabase db) {db.execsql (create_table_sql);} @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {//Db.execsql ("ALTER TABLE tb_student A DD COLUMN Other string ");//Schma_version}public void ExecuteSQL (STRING sql) {This.getwritabledaTabase (). execsql (SQL); This.getwritabledatabase (). Close (); /**** Query **/public arraylist<map<string,string>> getalldataindb (String State) {arraylist<map< string,string>> Listmap = new arraylist<map<string,string>> ();  Cursor cur = getwritabledatabase (). query (tb_name, NULL, NULL, null,null, NULL, NULL);//query and get cursor if (Cur.movetofirst ()) {// Determines whether the cursor is empty do {map<string,string> map=new hashmap<string, string> (), int id = cur.getint (cur.getcolumnindex ( dishmode.ds_id)); String statedb=cur.getstring (Cur.getcolumnindex (dishmode.ds_state)); if (Statedb.equals (state)) {string name= Cur.getstring (Cur.getcolumnindex (dishmode.ds_name)); String dishcover=cur.getstring (Cur.getcolumnindex (dishmode.ds_cover)); String dishtime=cur.getstring (Cur.getcolumnindex (Dishmode.ds_addtime)), Map.put ("id", id+ ""), Map.put ("style", myselfdishadapter.stylenormal+ ""); Map.put ("name", name), Map.put ("img", Dishcover); Map.put ("Addtime", dishtime); Listmap.add (map);}} while (Cur.movetonext ()); cur. Close (); This.getwritabledatabase (). Close (); return listmap;} /** * Inserting a piece of data; */public int Insert (Dishmode updata) {contentvalues CV = new Contentvalues (); Cv.put (Dishmode.ds_name, Updata.getname ()) ; Cv.put (Dishmode.ds_cover, Updata.getcover ()); Cv.put (Dishmode.ds_img, updata.getimg ()); Cv.put (DishMode.ds_state, Updata.getstate ()); Cv.put (Dishmode.ds_burden, Updata.getburden ()); Cv.put (Dishmode.ds_makes, UpData.getMakes ()); Cv.put (Dishmode.ds_tips, Updata.gettips ()); Cv.put (Dishmode.ds_addtime, Updata.getaddtime ()); Long id = This.getwritabledatabase (). Insert (Tb_name, NULL, CV), This.getwritabledatabase (). Close (); return (int) ID;} /** * Modify a piece of data; */public int update (Dishmode updata) {contentvalues CV = new Contentvalues (); Cv.put (Dishmode.ds_name, Updata.getname ()) ; Cv.put (Dishmode.ds_cover, Updata.getcover ()); Cv.put (Dishmode.ds_img, updata.getimg ()); Cv.put (DishMode.ds_state, Updata.getstate ()); Cv.put (Dishmode.ds_burden, Updata.getburden ()); Cv.put (Dishmode.ds_makes, UpData.getMakes ()); Cv.put (Dishmode.ds_Tips, Updata.gettips ()), Cv.put (Dishmode.ds_addtime, Updata.getaddtime ()), int row= this.getwritabledatabase (). Update (Tb_name, cv,null,null); This.getwritabledatabase (). Close (); return row;} /** * Delete */public boolean deletebyid (int id) {int i = this.getwritabledatabase (). Delete (Tb_name, dishmode.ds_id+ "=" + int Eger.valueof (ID) + "", null); This.getwritabledatabase (). Close (); if (i > 0) return True;elsereturn false;}


Android:sqliteopenhelper and Greendao Database operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.