android学习杂记.10
来源:互联网 发布:网贷大数据交易平台 编辑:程序博客网 时间:2024/05/16 08:12
本地数据库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
- android学习杂记.10
- android学习-----杂记一
- Android学习杂记
- Android 学习杂记
- Android学习杂记
- Android 学习杂记
- android学习杂记.1
- android学习杂记.2
- android学习杂记.3
- android学习杂记.4
- android学习杂记.5
- android学习杂记.6
- android学习杂记.8
- android学习杂记.9
- android学习杂记.11
- android学习杂记.12
- android学习杂记.13
- 学习杂记:android布局 LinearLayout
- Material Design之Toolbar的简单使用
- 通过摄像头捕获图像用tensorflow做手写数字识别
- AndroidStudio 用Gradle把Moudle打成jar包
- 【综合】WordPress代码的缩进与高亮处理
- 博弈论
- android学习杂记.10
- 关于本地servlet和线上servlet请求返回结果不一样的问题
- PHP快速推送微信模板消息
- 简单快速搭建tomcat可运行源码
- 软件测试
- Android恢复出厂设置
- 程序员面试100题之四:求1+2+...+n
- CI PHP7 session 不能读取的问题
- SSM框架——使用MyBatis Generator自动创建代码