Android也爲本地存儲提供了輕量級的數據庫存儲方案 -- SQLite數據庫存儲。
生成的數據庫文件存放在\data\data\com.xxx.test\databases\, 可以使用以下命令查詢生成的database。
假設database名稱爲BookStore.db, 表的名稱是Book。
進入adb shell,然後進入數據庫存放的位置\data\data\com.xxx.test\databases\。
輸入命令"sqlite3 BookStore.db", 即可進入sqlite的交互模式,常用命令如下,
1. 查詢表: sqlite> .table
2. 查詢表的生成命令:sqlite> .schema
3. 查詢表中的內容:sqlite> select * from Book;
4. 獲取命令幫助:sqlite> .help
通過上面的命令,我們就可以知道我們創建的數據庫/表格是否正確。
我們首先要創建一個新類,繼承SQLiteOpenHelper. 注意裏面的onUpgrade()方法,如果要更改數據庫的表格結構,需要先刪除之前的表格,才能更改原先的數據庫設計,這對於數據敏感性的程序,需要做數據庫的備份然後在升級數據庫結構。
package com.xxx.testsqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class MyDatebaseHelper extends SQLiteOpenHelper {
private static final String TAG = "TestSQLite";
public static final String CREATE_BOOK = "create table Book ("
+ "id integer primary key autoincrement, "
+ "author text, "
+ "price real, "
+ "pages integer, "
+ "name text)";
public static final String CREATE_CATEGORY = "create table Category ("
+ "id integer primary key autoincrement, "
+ "category_name text, "
+ "category_code integer)";
private Context mContext;
public MyDatebaseHelper(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
mContext = context;
Log.d(TAG, "MyDatebaseHelper: start");
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "onCreate: start");
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) {
Log.d(TAG, "onUpgrade: start");
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Category");
onCreate(db);
}
}
我們可以在主Activity的onCreate()方法中,創建並初始化數據庫,參考代碼如下,
private MyDatebaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MyDatebaseHelper(this, "BookStore.db", null, 2);
}
下面是數據庫的增加/修改/刪除/查詢的代碼,
private void addData() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
ContentValues values = new ContentValues();
// The 1st data
values.put("name", "The Da Vinci Code");
values.put("author", "Dan Brown");
values.put("pages", 454);
values.put("price", 16.96);
db.insert("Book", null, values);
values.clear();
// The 2nd data
values.put("name", "The Lost Symbol");
values.put("author", "Dan Brown");
values.put("pages", 510);
values.put("price", 19.35);
db.insert("Book", null, values);
}
private void updateData() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("price", 10.99);
db.update("Book", values, "name = ?", new String[] {"The Da Vinci Code"});
}
private void deleteData() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("Book", "pages > ?", new String[] {"500"});
}
private void queryData() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
// Query all of the data in the table "Book"
Cursor cursor = db.query("Book", null, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
// Go through the Cursor object, and print the data
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.d(TAG, "queryData: book name is " + name);
Log.d(TAG, "queryData: book author is " + author);
Log.d(TAG, "queryData: book pages is " + pages);
Log.d(TAG, "queryData: book price is " + price);
}while (cursor.moveToNext());
}
cursor.close();
}
有了數據庫的支持,Android的處理能力更加強大。