android:SQLiteOpenHelper 與 greenDao 資料庫操作

來源:互聯網
上載者:User

標籤:android資料庫   sqlite資料庫   greendao   sqliteopenhelper   android sqliteopenhe   

原文地址:http://blog.csdn.net/intbird

項目裡使用的是greeDao,sqllite是以前練習使用的,

如果項目裡全用這個,人會比較累;

然而greenDao使用起來非常方便,可以說是三分鐘速成

如不明白請看官方文檔,源碼裡也有詳細說明.

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

官網連結:http://greendao-orm.com/documentation/how-to-get-started/

官網DEMO:https://github.com/greenrobot/greenDAO

public static void save(Locations locs) {SQLiteDatabase db = Frame.getInstance().getDbHelp().getWritableDatabase();DaoMaster master = new DaoMaster(db);DaoSession session = master.newSession();DbLocationsDao locsDao = session.getDbLocationsDao();locsDao.insertOrReplace(locs); ;db.close();}public static void deletedFromLast(long lastId){SQLiteDatabase db = Frame.getInstance().getDbHelp().getWritableDatabase();DaoMaster master = new DaoMaster(db);DaoSession session = master.newSession();DbLocationsDao locsDao = session.getDbLocationsDao();QueryBuilder<DbLocations> query = locsDao.queryBuilder();DeleteQuery<DbLocations> delete = query.where(Properties.Time.le(lastId)).buildDelete();delete.executeDeleteWithoutDetachingEntities();db.close();}public static long getDbSize(){SQLiteDatabase db = Frame.getInstance().getDbHelp().getWritableDatabase();DaoMaster master = new DaoMaster(db);DaoSession session = master.newSession();DbLocationsDao locsDao = session.getDbLocationsDao();long size = locsDao.queryBuilder().count();return size;}public static List<DbLocations> getDataBySize(int size){SQLiteDatabase db = Frame.getInstance().getDbHelp().getWritableDatabase();DaoMaster master = new DaoMaster(db);DaoSession session = master.newSession();DbLocationsDao locsDao = session.getDbLocationsDao();List<DbLocations> list =  locsDao.queryBuilder().limit(size).orderAsc(Properties.Time).build().list();db.close();return list;}

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

@SqlLiteOpenHelper

例子

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.*;public class DishSqlite extends SQLiteOpenHelper {private static final String TB_NAME = "tb_dishMode";private static final String CREATE_TABLE_SQL = "create table "+TB_NAME+"("+ DishMode.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)";public DishSqlite(Context context) {super(context, 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 ADD COLUMN other STRING");// SCHMA_VERSION}public void executeSql(String sql) {this.getWritableDatabase().execSQL(sql);this.getWritableDatabase().close();}/**** 查詢**/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);// 查詢並獲得遊標if (cur.moveToFirst()) {// 判斷遊標是否為空白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;}/** * 插入一條資料; */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;}/** * 修改一條資料; */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;}/** * 刪除 */public boolean deleteById(int id) {int i = this.getWritableDatabase().delete(TB_NAME, DishMode.ds_id+"=" +  Integer.valueOf(id) + "",null);this.getWritableDatabase().close();if (i > 0)return true;elsereturn false;}


android:SQLiteOpenHelper 與 greenDao 資料庫操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.