原文地址: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 query = locsDao.queryBuilder();
DeleteQuery 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 getDataBySize(int size){
SQLiteDatabase db = Frame.getInstance().getDbHelp().getWritableDatabase();
DaoMaster master = new DaoMaster(db);
DaoSession session = master.newSession();
DbLocationsDao locsDao = session.getDbLocationsDao();
List 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