
目錄
1、介紹
2、路徑
3、創(chuàng)建數(shù)據(jù)庫
4、升級數(shù)據(jù)庫
5、添加數(shù)據(jù)
6、修改數(shù)據(jù)
7、刪除數(shù)據(jù)
8、查詢數(shù)據(jù)
-
介紹
SQLite是一款輕量級的關(guān)系型數(shù)據(jù)庫,它的運算速度非???,占用源很少,通常只需要幾百KB的內(nèi)存就足夠了,因而特別適合在移動設(shè)備上使用。
SQLite不僅支持標準的SQL語法,還遵循數(shù)據(jù)庫的ACID事務(wù),所以只要你以前使用過其他的關(guān)系型數(shù)據(jù)庫,就可以很快地上手 SQLite。
SQLite比一般的數(shù)據(jù)庫要簡單得多,它甚至不用設(shè)置用戶名和密碼就可以使用。
Android正是把這個功能極為強大的數(shù)據(jù)庫嵌入到了系統(tǒng)當中,使得本地持久化的功能有了一次質(zhì)的飛躍。
-
路徑
/data/data/<package name>/databases/
-
創(chuàng)建數(shù)據(jù)庫
Android提供了SQLiteOpenHelper幫助類用于實現(xiàn)數(shù)據(jù)庫的創(chuàng)建和升級
實現(xiàn)兩個抽象方法onCreate()、onUpgrade()
新建MyDataBaseHelper繼承SQLiteOpenHelper
public class MyDataBaseHelper extends SQLiteOpenHelper {
private Context mConetx;
public static String CREATE_BOOK = "create table Book(" +
"id integer primary key autoincrement," +
"author text," +
"name text)";
public MyDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
mConetx = context;
}
//創(chuàng)建數(shù)據(jù)庫
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
Toast.makeText(mConetx, "創(chuàng)建數(shù)據(jù)庫成功", Toast.LENGTH_SHORT).show();
}
//升級數(shù)據(jù)庫
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
創(chuàng)建數(shù)據(jù)庫
MyDataBaseHelper myDataBaseHelper = new MyDataBaseHelper(FileActivity.this, "DongBo.db", null, 1);
//創(chuàng)建數(shù)據(jù)庫
myDataBaseHelper.getWritableDatabase();
/**
*
* @param context 上下文
* @param name 數(shù)據(jù)庫名
* @param factory 允許我們在查詢數(shù)據(jù)的時候返回一個自定義的 Cursor,一般都是傳入 null
* @param version 版本號,若升級數(shù)據(jù)庫版本號需+1
*/
public MyDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
-
升級數(shù)據(jù)庫
創(chuàng)建新的表
public static final String CREATE_CITY = "create table City(" +
"id integer primary key autoincrement," +
"author text," +
"age integer," +
"city text)";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_CITY);
db.execSQL(CREATE_BOOK);
Toast.makeText(mConetx, "創(chuàng)建數(shù)據(jù)庫成功", Toast.LENGTH_SHORT).show();
}
將版本號遞增
MyDataBaseHelper myDataBaseHelper = new MyDataBaseHelper(FileActivity.this, "DongBo.db", null, 2);
問題一
我們會發(fā)現(xiàn)并沒有創(chuàng)建新的表,是因為只有第一次創(chuàng)建數(shù)據(jù)庫時會調(diào)用onCreate(),之后不會再調(diào)用onCreate()
修改
在onUpgrade()中判斷若當前表存在則刪除表之后再調(diào)用onCreate()
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists City");
db.execSQL("drop table if exists Book");
onCreate(db);
}
-
添加數(shù)據(jù)
SQLiteDatabase提供了insert()來進行數(shù)據(jù)的添加
/**
*
* @param table 表名
* @param nullColumnHack 用于在未指定添加數(shù)據(jù)的情況下給某些可為空的列自動賦值NULL,我們一般直接傳入null
* @param values 一個ContentValues對象,調(diào)用put方法來添加數(shù)據(jù)
* @return
*/
public long insert(String table, String nullColumnHack, ContentValues values) {}
mBtnSqInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues value = new ContentValues();
value.put("author", "羅貫中");
value.put("name", "三國演義");
db.insert("Book", null, value);
value.clear();
value.put("author", "曹雪芹");
value.put("name", "紅樓夢");
db.insert("Book", null, value);
}
});
-
修改數(shù)據(jù)
SQLiteDatabase提供了update()來進行數(shù)據(jù)的修改
/**
*
* @param table 表名
* @param values 一個ContentValues對象,將更新的數(shù)據(jù)添加進去,key需要對應數(shù)據(jù)庫中的key
* @param whereClause 修改的條件 key = ?
* @param whereArgs 修改條件的值,按照修改的條件依次順序
* @return
*/
public int update(String table, ContentValues values, String whereClause, String[] whereArgs) {}
mBtnSqUpdateData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db = myDataBaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "董博");
db.update("Book", values, "author = ?", new String[]{"羅貫中"});
}
});
-
刪除數(shù)據(jù)
SQLiteDatabase提供了delete()來進行數(shù)據(jù)的刪除
/**
*
* @param table 表明
* @param whereClause 刪除的條件 key = ?
* @param whereArgs 刪除條件的值,按照修改的條件依次順序
* @return
*/
public int delete(String table, String whereClause, String[] whereArgs) {}
mBtnSqDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db = myDataBaseHelper.getWritableDatabase();
db.delete("Book", "id = ?", new String[]{"1"});
}
});
-
查詢數(shù)據(jù)
SQLiteDatabase提供了query()來進行數(shù)據(jù)的查詢
/**
*
* @param table 表明
* @param columns 指定查詢的列名(默認所有列)
* @param selection 用于約束查詢某一行或某幾行的數(shù)據(jù)(默認查詢所有行)
* @param selectionArgs 用于約束查詢某一行或某幾行的數(shù)據(jù)(默認查詢所有行)
* @param groupBy 指定需要group by的列,不指定則不進行g(shù)roup by操作
* @param having 對group by之后的數(shù)據(jù)進一步過濾
* @param orderBy 指定查詢結(jié)果的排序方式
* @return
*/
public Cursor query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy) {}
mBtnSqQuery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db = myDataBaseHelper.getWritableDatabase();
Cursor cursor = db.query("Book", null, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
String author = cursor.getString(cursor.getColumnIndex("author"));
String name = cursor.getString(cursor.getColumnIndex("name"));
Log.e("author", author);
Log.e("name", name);
} while (cursor.moveToNext());
}
cursor.close();
}
});
這里除過第一個參數(shù)表名天界,其他的傳null,表示查詢所有數(shù)據(jù),查詢之后的到Cursor對象,先調(diào)用moveToFirst()將指針移到第一條,然后通過循環(huán)遍歷每條數(shù)據(jù),通過cursor.getColumnIndex()獲取表中對應的位置索引,然后傳入索引獲取值,最后調(diào)用close()關(guān)閉Cursor。
這篇只是對數(shù)據(jù)庫的一個簡單的使用,還有很多需要學習的地方,像sqlite語句的寫法還需要好好的研究一波,在項目中一般前端用到數(shù)據(jù)庫的地方不是很常見,但是多學習一些,對我們也有好處,大家可以看下GreenDao
若有不足,請大家指教
數(shù)據(jù)存儲相關(guān)文章
Android數(shù)據(jù)存儲之文件存儲
Android數(shù)據(jù)存儲之SharedPreferences
Android數(shù)據(jù)存儲之SQLite存儲