android:SQLiteOpenHelper 与 greenDao 数据库操作

原文地址: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);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_TABLE_SQL);
	}

	@Override
	public 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;
		else
			return false;
	}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。