android学习杂记.10

本地数据库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工具类 在里面写增删改查方法

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

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值