SQLite
轻量级的关系型数据库
Android系统内置了一款轻量级的关系型数据库,叫作SQLite,其运行速度快,占用资源很少(通常是几百KB内存),不仅支持标准SQL语法,还支持ACID事务
基本数据类型:
- integer 整型
- real 浮点型
- text 文本
- blob 二进制类型
如何使用SQLite
创建与更新数据库
Android提供专门的管理类SQLiteOpenHelper,我们需要创建自己的类来继承它,并重写onCreate()和onUpgrade()方法,分别用于创建和升级数据库的逻辑.。SQLiteOpenHelper还提供两个实例方法,getReadableDatabase()和getWritableDatabase(),均返回可对数据库进行读写的对象,当数据库不可写入时(磁盘已满),前者返回对数据库只可读的对象,后者会出现异常
- 构造函数
public XXXXXXX(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
构建SQLiteOpenHelpers实例之后调用getReadableDatabase()或getWritableDatabase()就可创建数据库了,数据库文件会存放在/data/data/包名/databses/目录下
- 创建与更新数据库的逻辑
@Override
public void onCreate(SQLiteDatabase db) {}
在执行getXXXDatabase()后会执行onCreate()来完成建表工作,当需要创建的表已存在时,系统不会再调用onCreate(),此时需要新建另一张表该怎么办呢?
调用以下方法即可,如何调用?
修改创建SQLiteOpenHelper实例时的版本参数version即可
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){}
CRUD
Create(添加)
SQLiteDatabase db = databaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("author","Dan Brown");
values.put("pages",454);
values.put("price",16.96);
values.put("name","The Da Vinci Code");
db.insert("Book",null,values);
values.clear();
values.put("author","Dan Brown");
values.put("pages",554);
values.put("price",16.96);
values.put("name","The Lost Symble");
db.insert("Book",null,values);
Retrieve(查询)
SQLiteDatabase db = databaseHelper.getWritableDatabase();
Cursor cursor = db.query("Book",null,null,null,null,null,null);
if(cursor.moveToFirst()){
do{
String name = cursor.getString(cursor.getColumnIndex("name"));
String author = cursor.getString(cursor.getColumnIndex("author"));
int pages = cursor.getInt(cursor.getColumnIndex("pages"));
double price = cursor.getDouble(cursor.getColumnIndex("price"));
Log.e(TAG, "onClick: " + name );
Log.e(TAG, "onClick: " + author );
Log.e(TAG, "onClick: " + pages );
Log.e(TAG, "onClick: " + price);
}while(cursor.moveToNext());
}
Update(更新)
SQLiteDatabase db = databaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("price",10.99);
//第三第四个参数决定哪一(些)行数据被更新,第三个参数表示where,?为占位符,内容由第四个参数填充
db.update("Book",values1,"name = ?",new String[]{
"The Da Vinci Code"
});
Delete(删除)
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.delete("Book","pages>?",new String[]{
"500"
});
获取数据库对象,创建ContentValues实例,将数据放入ContentValues实例中,插入,更新;
查询时,query()方法返回Cursor对象,调用它的moveToFirst()将数据的指针指向第一行,然后进入一个循环,去遍历查询到的每一行数据,
moveToNext()方法判断遍历是否结束
查看数据库表是否创建成功
- 打开cmd,输入adb shell
- 显示超级管理员“#”,若为“$”,输入su切换成超级管理员
- cd /data/data/包名/databases/ 切换路径
- ls 查看该目录下的文件
- sqlite3 数据库名 打开数据库
- .table 查看数据库中的表
- select * from table 查询表中数据
- .schema 查看建表语句
BookStore.db-journal 是为了让数据库支持事务而创建的临时日志文件,通常大小为0k
android_metadata表是每个数据库都会生成的
使用SQL操作数据库
db.execSQL("insert into Book(name,author,pages,price) values(?,?,?,?)",
new String[]{"The Da Vinci Code","Dan Brown","454","16.96"});
db.execSQL("update Book set price = ? where name = ?",
new String[]{"10.99","The Da Vinci Code"});
db.execSQL("delete from Book where pages > ?",new String[]{"500"});
db.rawQuery("select * from Book",null);
除了查询语句使用rawQuery()外,其他均使用execSQL()
最后附上MyDatabaseHelper代码
public class MyDatabaseHelper extends SQLiteOpenHelper {
private Context mContext;
//SQL语句
private static final String CREATE_BOOK = "create table Book("
+ "id integer primary key autoincrement,"
+ "author text,"
+ "price real,"
+ "pages integer,"
+ "name text)";
private static final String CREATE_CATEGORY = "create table Category("
+ "id integer primary key autoincrement,"
+ "category_name text,"
+ "category_code integer)";
//构造函数
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
mContext = context;
}
//getXXXDatabase()后调用
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_CATEGORY);
Toast.makeText(mContext,"Create succeeded",Toast.LENGTH_SHORT).show();
}
//更新数据库
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists Book");//删除表
db.execSQL("drop table if exists Category");
onCreate(db);
}
}
注:更新数据库
private MyDatabaseHelper databaseHelper;
databaseHelper = new MyDatabaseHelper(this,"BookStore.db",null,1);
修改参数version
databaseHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);
调用onUpgrade()
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//删除数据库
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Category");
onCreate(db);
}