Android開發(fā)關于SQLite的基礎知識

什么是SQLite?

SQLite是一個小型的、可嵌入、開源的關系型數(shù)據(jù)庫,因它的系統(tǒng)開銷非常小,檢索的效率非常高,所以被廣泛的應用。

SQLite數(shù)據(jù)庫數(shù)據(jù)類型

Integer varchar(10) float double char(100) text

經常使用的sql語句

1.創(chuàng)建表的語句
create table 表名(字段名稱 數(shù)據(jù)類型 約束,字段名稱 數(shù)據(jù)類型 約束......)

例子:create table student(_id Integer primary key,name varchar(10),age Integer not null)

2.刪除表的語句
drop table 表名

例子:drop table student ----刪除student表

3.插入數(shù)據(jù)
insert into 表名[字段,字段] values(值1,值2......)

例子:
insert into student(_id,age) values(1,20) ----插入一條id=1,age=20的數(shù)據(jù)

insert into student values(2,"zhangsan",30) ----插入一條id=2,name="zhangsan",age=30的數(shù)據(jù)

4.修改數(shù)據(jù)
update 表名 set 字段=新值 where 修改條件

例子:update student set name="lisi",age=20 where _id=1 ----將id=1的這條數(shù)據(jù)的name字段的值修改為lisi,age字段的值修改為20

5.刪除數(shù)據(jù)
delete from 表名 where 刪除的條件

例子:
delete from student where _id=2 ----刪除student表中id=2的這條數(shù)據(jù)
delete from student ----刪除student表中所有的數(shù)據(jù)

6.查詢數(shù)據(jù)
select 字段名 from 表名 where 查詢條件 group by 分組的字段 having 篩選條件 order by 排序字段

例子:
select *from student ----查詢student表中的所有數(shù)據(jù)

select _id,name from student ----查詢student表中的_id和name這兩個字段

select *from student where _id=1 ----查詢student表中的_id=1的數(shù)據(jù)

select *from student where _id<>1 ----查詢student表中的_id不等于1的數(shù)據(jù)(<>代表不等于)

select *from student where _id=1 and age>18 ----查詢student表中的_id=1,age>18的數(shù)據(jù)

select *from student where name like "%小%" ----查詢student表中name字段中中間含有“小”字的數(shù)據(jù),“小”字前面和后面可以是任意的字符

select *from student where name like "_小%" ----查詢student表中name字段中一個字符之后含有“小”字的數(shù)據(jù),“小”字后面可以是任意的字符

select *from student where name is null ----查詢student表中name字段為null的數(shù)據(jù)

select *from student where age between 10 and 20 ----查詢student表中age字段的值為10-20的數(shù)據(jù)

select *from student where age>18 order by ----查詢student表中age的值大于18的數(shù)據(jù),并根據(jù)_id進行排序

select * from student limit 1,20 ----查詢student表中第一頁的20條數(shù)據(jù)(數(shù)據(jù)庫數(shù)據(jù)分頁顯示的時候使用)

Android中SQLite的使用

1.繼承SQLiteOpenHelper類,并實現(xiàn)其中的方法

/**
* SQLiteOpenHelper
* 1.提供了onCreate() onUpgrade()等創(chuàng)建數(shù)據(jù)庫更新數(shù)據(jù)庫的方法
* 2.提供了獲取數(shù)據(jù)庫對象的函數(shù)
*/
public class MySqliteHple extends SQLiteOpenHelper{

    public MySqliteHple(Context context) {
        super(context, Constant.DATABASE_NAME, null, Constant.DATABASE_VERSION);
    }

    /**
     * 構造函數(shù)
     * @param context 上下文對象
     * @param name 表示創(chuàng)建數(shù)據(jù)庫的名稱
     * @param factory 游標工廠
     * @param version 表示創(chuàng)建數(shù)據(jù)庫的版本 >=1
     */
     public MySqliteHple(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
         super(context, name, factory, version);
     }

    /**
     * 當數(shù)據(jù)庫創(chuàng)建時回調的函數(shù)
     * @param db 數(shù)據(jù)庫對象
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i("tag","------onCreate-------");
        String sql="create table student(_id Integer primary key,name varchar(10),age Integer not null)";

        Log.i("tag","sql:"+sql);

        db.execSQL(sql);//執(zhí)行sql語句
    }

    /**
     * 當數(shù)據(jù)庫版本更新時回調的函數(shù)
     * @param db 數(shù)據(jù)庫對象
     * @param oldVersion 數(shù)據(jù)庫舊版本
     * @param newVersion 數(shù)據(jù)庫新版本
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("tag","------onUpgrade-------");
    }

    /**
     * 當數(shù)據(jù)庫打開時回調的函數(shù)
     * @param db 數(shù)據(jù)庫對象
     */
    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        Log.i("tag","------onOpen-------");
    }
}

2.通過SQLiteOpenHelper獲取數(shù)據(jù)庫SQLiteDatabase對象

//getReadableDatabase() getWritableDatabase() 創(chuàng)建或打開數(shù)據(jù)庫,如果數(shù)據(jù)庫不存在則創(chuàng)建數(shù)據(jù)庫,如果數(shù)據(jù)庫
//存在則直接打開數(shù)據(jù)庫。默認情況下兩個函數(shù)都表示打開或者創(chuàng)建可讀可寫的數(shù)據(jù)庫對象,如果磁盤已滿或者數(shù)據(jù)庫本身權限等
//情況下getReadableDatabase()打開的是只讀數(shù)據(jù)庫
SQLiteDatabase db=mHple.getWritableDatabase();

3.增刪改查
使用sql語句操作:

//插入數(shù)據(jù)
String sql="insert into student values(1,'小白',30)";
db.execSQL(sql);

//更新數(shù)據(jù)
String sql="update set student name='小黑' where _id=1";
db.execSQL(sql);

//刪除數(shù)據(jù)
String sql="delete from student where _id=1";
db.execSQL(sql);

//查詢數(shù)據(jù)
String sql="select * from student";
cursor=db.rawQuery(sql,null);

使用Api進行操作:

插入數(shù)據(jù)

/**
* insert(String table, String nullColumnHack, ContentValues values)
*
* String table 表示插入數(shù)據(jù)表的名字
* String nullColumnHack SQL要求插入的數(shù)據(jù)不能全為null,但有些字段可以為null。一般這個參數(shù)我們直接給null
* ContentValues values 鍵為String類型的HashMap集合
* 返回值為long類型  表示插入數(shù)據(jù)的列數(shù) 如果值為-1則表示插入失敗
*/
insert(String table, String nullColumnHack, ContentValues values)

更新數(shù)據(jù)

/**
* update(String table, ContentValues values, String whereClause, String[] whereArgs)
*
* String table 表示修改數(shù)據(jù)表的名字
* ContentValues values 鍵為String類型的HashMap集合
* String whereClause 表示修改條件
* String[] whereArgs 表示修改條件的占位符
*/
update(String table, ContentValues values, String whereClause, String[] whereArgs)

刪除數(shù)據(jù)

/**
* delete(String table, String whereClause, String[] whereArgs)
* String table 表示刪除數(shù)據(jù)表的名字
* String whereClause 表示刪除條件
* String[] whereArgs 表示刪除條件的占位符
*/
delete(String table, String whereClause, String[] whereArgs)

查詢數(shù)據(jù)

/**
* query(String table, String[] columns, String selection,
* String[] selectionArgs, String groupBy, String having,
* String orderBy)
*
* String table 表示查詢的表名
* String[] columns 表示查詢的表中的字段名字 null查詢所有
* String selection 表示查詢條件 where子句
* String[] selectionArgs 表示查詢條件占位符的取值
* String groupBy 表示分組條件 group by子句
* String having 表示篩選條件 having子句
* String orderBy 表示排序條件 order by子句
*
*/
query(String table, String[] columns, String selection,
             String[] selectionArgs, String groupBy, String having,
             String orderBy)

SQLite事務的使用

1.數(shù)據(jù)庫顯式開啟事務
db.beginTransaction();

2.提交當前事務
db.setTransactionSuccessful();

3.關閉事務
db.endTransaction();

例如我要插入大量數(shù)據(jù)的時候可以使用:

db.beginTransaction();
for (int i=0;i<30;i++){
    sql="insert into person values("+i+",'小白"+i+"',31)";
    DbManger.execSQL(db,sql);
}
db.setTransactionSuccessful();
db.endTransaction();

SQLite數(shù)據(jù)庫分頁

當數(shù)據(jù)庫中含有大量數(shù)據(jù)時,如果一次性加載的程序中很容易造成程序崩潰、卡頓。為了提高用戶體驗,我們需將數(shù)據(jù)庫進行分頁操作。

//主要使用一下sql語句
select * from student limit ?,?

具體可以 查看demo:

SQLite操作

demo地址:SQLite demo

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

相關閱讀更多精彩內容

友情鏈接更多精彩內容