您的位置:首页 > 其它

个人技术博客(α)

2017-11-16 20:10 183 查看

Sqlite数据库的学习

Android Studio中有SharedPreferences存储数据、文件存储数据、Sqlite数据库存储等存储数据的方式。在α冲刺阶段中,我是做本地数据库的,所以主要是以Sqlite数据库的学习为主。

Sqlite介绍

Sqlite是轻量级嵌入式数据库引擎,它支持 SQL 语言,并且只利用很少的内存就有很好的性能。现在的主流移动设备像Android、iPhone等都使用Sqlite作为复杂数据的存储引擎.


数据操作

SQLiteDatabase类为我们提供了很多种方法,上面的代码中基本上囊括了大部分的数据库操作;对于添加、更新和删除来说,我们都可以使用

db.executeSQL(String sql);
db.executeSQL(String sql, Object[] bindArgs);//sql语句中使用占位符,然后第二个参数是实际的参数集

除了统一的形式之外,他们还有各自的操作方法:

db.insert(String table, String nullColumnHack, ContentValues values);
db.update(String table, Contentvalues values, String whereClause, String whereArgs);
db.delete(String table, String whereClause, String whereArgs);


数据的添加

1.使用insert方法

ContentValues cv = new ContentValues();//实例化一个ContentValues用来装载待插入的数据
cv.put("title","you are beautiful");//添加title
cv.put("weather","sun"); //添加weather
cv.put("context","xxxx"); //添加context
String publish = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date());
cv.put("publish ",publish); //添加publish
db.insert("diary",null,cv);//执行插入操作


2.使用execSQL方式来实现

String sql = "insert into user(username,password) values ('Jack Johnson','iLovePopMuisc');//插入操作的SQL语句
db.execSQL(sql);//执行SQL语句


数据删除

1.使用delete方法

String whereClause = "username=?";//删除的条件
String[] whereArgs = {"Jack Johnson"};//删除的条件参数
db.delete("user",whereClause,whereArgs);//执行删除


2.使用execSQL方式的实现

String sql = "delete from user where username='Jack Johnson'";//删除操作的SQL语句
db.execSQL(sql);//执行删除操作


数据修改

1.使用update方法

ContentValues cv = new ContentValues();//实例化ContentValues
cv.put("password","iHatePopMusic");//添加要更改的字段及内容
String whereClause = "username=?";//修改条件
String[] whereArgs = {"Jack Johnson"};//修改条件的参数
db.update("user",cv,whereClause,whereArgs);//执行修改


2.使用execSQL方式的实现

String sql = "update user set password = 'iHatePopMusic' where username='Jack Johnson'";//修改的SQL语句
db.execSQL(sql);//执行修改


数据查询

查询操作相对于上面的几种操作要复杂些,因为我们经常要面对着各种各样的查询条件,所以系统也考虑到这种复杂性,为我们提供了较为丰富的查询形式:

db.rawQuery(String sql, String[] selectionArgs);
db.query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy,                      String having, String orderBy);
db.query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit);
db.query(String distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit);


Cusor介绍

Cusor是Sqlite查询中会返回的一个游标对象,下列是对游标的常用语句:
- c.move(int offset); //以当前位置为参考,移动到指定行
- c.moveToFirst();    //移动到第一行
- c.moveToLast();     //移动到最后一行
- c.moveToPosition(int position); //移动到指定行
- c.moveToPrevious(); //移动到前一行
- c.moveToNext();     //移动到下一行
- c.isFirst();        //是否指向第一条
- c.isLast();     //是否指向最后一条
- c.isBeforeFirst();  //是否指向第一条之前
- c.isAfterLast();    //是否指向最后一条之后
- c.isNull(int columnIndex);  //指定列是否为空(列基数为0)
- c.isClosed();       //游标是否已关闭
- c.getCount();       //总数据项数
- c.getPosition();    //返回当前游标所指向的行数
- c.getColumnIndex(String columnName);//返回某列名对应的列索引值
- c.getString(int columnIndex);   //返回当前行指定列的值


SqliteOpenHelper类介绍

SQLiteOpenHelper是SQLiteDatabase的一个帮助类,用来管理数据库的创建和版本的更新。一般是建立一个类继承它,并实现它的onCreate和onUpgrade方法。这样可以简化对数据库的操作。其构造方法为SQLiteOpenHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version),构造方法中context :程序上下文环境 即:XXXActivity.this;name :数据库名字,factory:游标工厂,默认为null,即为使用默认工厂;version:数据库版本号。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: