Android 數(shù)據(jù)庫SQLite使用小結(jié)

前言

在Android開發(fā)過程中,對(duì)于數(shù)據(jù)的存儲(chǔ),我們或多或少的都會(huì)使用到數(shù)據(jù)庫相關(guān)的操作,所以在此小小的總結(jié)一下,Android中使用SQLite數(shù)據(jù)庫的技巧和方法,算是自己對(duì)數(shù)據(jù)庫知識(shí)的復(fù)習(xí)。項(xiàng)目源代碼
本博客同步發(fā)布于XueLong的博客

增刪改查

adb shell中操作SQLite

adb shell
su  //以管理員的身份運(yùn)行命令
sqlite DATABASE_NAME.db //進(jìn)入要操作的數(shù)據(jù)庫
.table  //查看當(dāng)前數(shù)據(jù)庫有哪些表
.table t% //".tables"命令后也可以跟一參數(shù),它是一個(gè)pattern,這樣命令就只列出表名和該參數(shù)匹配的表。
.schema //顯示最初用于創(chuàng)建數(shù)據(jù)庫的CREATE TABLE和CREATE INDEX的SQL語句
.schema t% //".schema"命令可以包含一個(gè)參數(shù),它是一個(gè)pattern,用于對(duì)表進(jìn)行過濾,這時(shí)只會(huì)顯示滿足條件的表和所有索引的SQL語句
pragma table_info(TABLE_NAME);  //查看表的數(shù)據(jù)結(jié)構(gòu)
.mode line  //切換顯示模式,可用參數(shù)有 line list column
.separator |   //字段之間使用 | 間隔開
.output filename.txt  //將數(shù)據(jù)庫中的數(shù)據(jù)輸出到文件中
.width 12 6 //調(diào)整列寬 12表示第一列寬為12,6表示第二列寬為6
.databases //顯示所有當(dāng)前連接打開的數(shù)據(jù)庫的一個(gè)列表,main:最初打開的數(shù)據(jù)庫,temp:臨時(shí)表的數(shù)據(jù)庫
.exit //退出

小技巧

  • SQLite EXpert Personal 中注釋SQL語句,單行注釋使用-- ,多行注釋使用/**/
  • 每個(gè) SQLite 數(shù)據(jù)庫中都有一個(gè)隱藏的 sqlite_master 表,它記載了當(dāng)前數(shù)據(jù)庫中所有的建表語句。
  • 在對(duì)數(shù)據(jù)庫進(jìn)行增刪改查前首先需要先調(diào)用SQLiteOpenHelpergetReadableDatabase()(查時(shí)調(diào)用)或getWritableDatabase()(增刪改時(shí)調(diào)用)方法。

常規(guī)的SQL語句操作

SQLiteDatabase db = null;
//在執(zhí)行增刪改語句時(shí)使用
db = helper.getWritableDatabase();
//在執(zhí)行查詢語句時(shí)使用
//db = helper.getReadableDatabase();
db.execSQL("在這里填入你的SQL語句");

常用的SQL語句如下:

//建表語句
create table if not exists TABLE_NAME(Id integer orimary key,Name text,Age integer);
//增加一條數(shù)據(jù)
insert into TABLE_NAME(Id,Name,Age) values (1,'lixuelong',23);
//刪除一條數(shù)據(jù)
delete from TABLE_NAME where Name = 'lixuelong';
//修改一條數(shù)據(jù)
update TABLE_NAME set Name = 'xuelong' where Id =1;
//查詢語句
select * from TABLE_NAME where Name = 'lixuelong' order by Age desc;
//統(tǒng)計(jì)查詢語句
select count(Id) from TABLE_NAME where Age = 18;
//比較查詢語句
select Id,Name,Max(Age) as Age from TABLE_NAME;

Android自有的事務(wù)(Transaction)處理方法

增加數(shù)據(jù)操作
long insertOrThrow(String table, String nullColumnHack, ContentValues values)
無論第三個(gè)參數(shù)是否包含數(shù)據(jù),執(zhí)行此方法必定會(huì)插入一條數(shù)據(jù)

  • table:表名
  • nullColumnHack:用于指定空值字段的名稱
  • values:要存放的ContentValues對(duì)象,可以為null
  • 返回值:返回新添記錄的行號(hào),與主鍵id無關(guān),發(fā)生錯(cuò)誤返回-1
SQLiteDatabase db = null;
db = helper.getWritableDatabase();
db.beginTransaction();
// insert into TABLE_NAME (_id,Name,Age) values (6,'張三',18);
ContentValues contentValues = new ContentValues();
contentValues.put("_id","6");
contentValues.put("Name", "張三");
contentValues.put("Age", 18);
db.insertOrThrow(TABLE_NAME, null, contentValues);
db.setTransactionSuccessful();

刪除數(shù)據(jù)操作
int delete(String table, String whereClause, String[] whereArgs)

  • table:表名
  • whereClause:刪除的條件,如果為null,則整行刪除
  • whereArgs:字符串?dāng)?shù)組,和whereClause配合使用,有兩種使用方法,1、如果whereClause的條件已經(jīng)直接給出,如"Name='張三'",則whereArgs可以設(shè)為null。2、如果whereClause的條件沒有直接給出,如"Name=?",則?會(huì)被whereArgs字符串?dāng)?shù)組中的值代替。
  • 返回值:0:刪除是被,1:刪除成功
SQLiteDatabase db = null;
db = helper.getWritableDatabase();
db.beginTransaction();
db.delete(TABLE_NAME, "Name=?", new String[]{"張三"});
//db.execSQL("delete from TABLE_NAME where Name = '張三';");
db.setTransactionSuccessful();

修改數(shù)據(jù)操作
update(String table, ContentValues values, String whereClause, String[] whereArgs)

  • table:表名
  • values:要存放的ContentValues對(duì)象,可以為null
  • whereClause:修改的條件,如果為null,則整行修改
  • whereArgs:字符串?dāng)?shù)組,和whereClause配合使用,有兩種使用方法,1、如果whereClause的條件已經(jīng)直接給出,如"Name='張三'",則whereArgs可以設(shè)為null。2、如果whereClause的條件沒有直接給出,如"Name=?",則?會(huì)被whereArgs字符串?dāng)?shù)組中的值代替。
  • 返回值:the number of rows affected
SQLiteDatabase db = null;
db = helper.getWritableDatabase();
db.beginTransaction();
// update TABLE_NAME set Name = 'zhangsan' where Name = '張三'
ContentValues contentValues = new ContentValues();
contentValues.put("Name", "zhangsan");
db.update(TABLE_NAME, contentValues, "Name = ?", new String[]{"張三"});
db.setTransactionSuccessful();

查詢數(shù)據(jù)操作
query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
query方法較多,這里只列舉一個(gè),有興趣的朋友可以自行研究

  • table:表名
  • columns:需要返回的列的列表,如果為null,則返回全部的列
  • selection:查詢的條件,符合什么條件的行將返回。如果為null,則這個(gè)表里的所有行都將返回。其兩種用法和update里的一樣
  • selectionArgs:用法和update里的一樣
  • groupBy:用于控制分組
  • having:用于對(duì)分組進(jìn)行過濾
  • orderBy:用于對(duì)記錄進(jìn)行排序
  • 返回值:Cursor對(duì)象
SQLiteDatabase db = null;
Cursor cursor = null;
db = helper.getWritableDatabase();
// select * from TABLE_NAME where Age = 18
cursor = db.query(TABLE_NAME, TABLE_COLUMNS, "Age = ?", new String[]{String.valueOf(18)},null, null, null);
if (cursor.getCount() > 0) {
   List<DBSQLBean> beanList = new ArrayList<DBSQLBean>();
   while (cursor.moveToNext()) {
        DBSQLBean bean = parseBean(cursor);
        beanList.add(bean);
    }
    return beanList;
}

統(tǒng)計(jì)查詢數(shù)據(jù)操作

int count = 0;
SQLiteDatabase db = null;
Cursor cursor = null;
db = helper.getWritableDatabase();
// select count(_id) from TABLE_NAME where Age = 18
cursor = db.query(DBSQLHelper.TABLE_NAME, new String[]{"COUNT(_id)"},"Age = ?", new String[]{String.valueOf(18)},null, null, null);
if (cursor.moveToFirst()) {
    count = cursor.getInt(0);
}

比較查詢數(shù)據(jù)操作

SQLiteDatabase db = null;
Cursor cursor = null;
db = helper.getWritableDatabase();
// select _id,Name,Max(Age) as Age from TABLE_NAME
cursor = db.query(DBSQLHelper.TABLE_NAME, new String[]{"_id", "Name","Max(Age) as Age"},null, null, null, null, null);
if (cursor.getCount() > 0) {
    if (cursor.moveToFirst()) {
        return parseBean(cursor);
    }
}

寫在最后

以上就是對(duì)Android中數(shù)據(jù)庫操作的小小總結(jié)。

如果你在參考過程中遇到問題,可以在我的聯(lián)系方式中給我提問。

后面會(huì)繼續(xù)介紹,Android的相關(guān)知識(shí),歡迎繼續(xù)關(guān)注我博客的更新。

項(xiàng)目源代碼

參考資源

轉(zhuǎn)載請(qǐng)注明:XueLong的博客 ? Android 數(shù)據(jù)庫SQLite使用小結(jié)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容