您的位置:首页 > 移动开发 > Android开发

android学习杂记.10

2017-04-14 14:17 260 查看
本地数据库sqlite的简单用法

先创建一个类继承  SQLiteOpenHelper

public class SqlLiteOpHelper extends SQLiteOpenHelper {
//数据库名字
private static final String DATABASE_NAME="swJAB";
//数据库版本
private static final int DATABASE_VERSION=1;

private static SqlLiteOpHelper sSqlLiteOpHelper;
public SqlLiteOpHelper(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}

public synchronized static SqlLiteOpHelper getInstance(Context context)
{
if(null==sSqlLiteOpHelper)
{
sSqlLiteOpHelper=new SqlLiteOpHelper(context);

}
return  sSqlLiteOpHelper;
}
public synchronized static void destoryInstance() {
if (null != sSqlLiteOpHelper) {
sSqlLiteOpHelper.close();
}
}
public static final String WALK_COUNT="WALK_COUNT";
public static final String YESTDAY_DATE="YESTDAY_DATE";
public static final String BEI_WANG_LU="BEI_WANG_LU";

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table "+WALK_COUNT+" ( _ID integer primary key autoincrement,"
+"allcount integer not null,lastdate bigint not null )"
);

sqLiteDatabase.execSQL("create table "+YESTDAY_DATE+" ( _ID integer primary key autoincrement,"
+"yesterday bigint not null )"
);
sqLiteDatabase.execSQL("create table "+BEI_WANG_LU+" ( _ID integer primary key autoincrement,"
+"title text not null, "+
"content text not null,"+
"date text not null"+
")"
);

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
}

app在安装的时候在oncreate里面执行语句 创建了3张表

再创建一个sqlite工具类 在里面写增删改查方法

/**
*   向表里面写数据
*/

[b]public boolean addBeiWangLu(BeiWangLu beiWangLu){
ContentValues contentValues=new ContentValues();
contentValues.put("title",beiWangLu.getTitle());
contentValues.put("content",beiWangLu.getContent());
contentValues.put("date",beiWangLu.getDate());
long rows= MyApplication.getWriteableInstance().insert(SqlLiteOpHelper.BEI_WANG_LU,null,contentValues);
if (rows > 0) {
return true;
}
return false;
}

/**
*    从表里面拿所有数据,按id降序 排序
*/
public List<BeiWangLu> getBeiWangLus(){

List<BeiWangLu> lists=new ArrayList<BeiWangLu>();
Cursor cursor= MyApplication.getReadableInstance().query(SqlLiteOpHelper.BEI_WANG_LU,null,null,null,null,null,"_ID desc");
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
BeiWangLu beiWangLu=new BeiWangLu();
beiWangLu.setId(cursor.getInt(cursor.getColumnIndexOrThrow("_ID")));
beiWangLu.setTitle(cursor.getString(cursor.getColumnIndexOrThrow("title")));
beiWangLu.setContent(cursor.getString(cursor.getColumnIndexOrThrow("content")));
beiWangLu.setDate(cursor.getString(cursor.getColumnIndexOrThrow("date")));
lists.add(beiWangLu);
}

return  lists;
}


/**
*    从表里面拿一条数据 根据id
*/
public BeiWangLu getOneBeiWangLus(int id){

Cursor cursor= MyApplication.getReadableInstance().query(SqlLiteOpHelper.BEI_WANG_LU,null,"_ID="+id,null,null,null,null);
cursor.moveToFirst();
BeiWangLu beiWangLu=new BeiWangLu();
beiWangLu.setId(cursor.getInt(cursor.getColumnIndexOrThrow("_ID")));
beiWangLu.setTitle(cursor.getString(cursor.getColumnIndexOrThrow("title")));
beiWangLu.setContent(cursor.getString(cursor.getColumnIndexOrThrow("content")));
beiWangLu.setDate(cursor.getString(cursor.getColumnIndexOrThrow("date")));

return  beiWangLu;
}

/**
* 删除表里一条数据 根据id
*/
public boolean deleteOneBeiWangLu(int id){

int  rows=  MyApplication.getWriteableInstance().delete(SqlLiteOpHelper.BEI_WANG_LU,"_ID="+id,null);
if(rows>0)
{
return true;
}
return  false;
}

/**
* 删除表里所有数据
*/
public  synchronized boolean  deleteJiBuAndAdd(JiBu jiBu){

int  rows=  MyApplication.getWriteableInstance().delete(SqlLiteOpHelper.WALK_COUNT,null,null);

if(rows>0)
return true;

return  false;
}

/**
* 修改表里面的数据 根据id
*/

public boolean upDateJiBu(JiBu jiBu){
ContentValues contentValues=new ContentValues();
contentValues.put("allcount",jiBu.getCount());
contentValues.put("lastdate",jiBu.getLastDate());
int  rows=  MyApplication.getWriteableInstance().update(SqlLiteOpHelper.WALK_COUNT,contentValues,"_ID=1",null);
if (rows > 0) {
return true;
}
return false;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: