什么是SQLite?
SQLite是一個小型的、可嵌入、開源的關系型數據庫,因它的系統開銷非常小,檢索的效率非常高,所以被廣泛的應用。
SQLite數據庫數據類型
Integer varchar(10) float double char(100) text
經常使用的sql語句
1.創建表的語句
create table 表名(字段名稱 數據類型 約束,字段名稱 數據類型 約束......)
例子:create table student(_id Integer primary key,name varchar(10),age Integer not null)
2.刪除表的語句
drop table 表名
例子:drop table student ----刪除student表
3.插入數據
insert into 表名[字段,字段] values(值1,值2......)
例子:
insert into student(_id,age) values(1,20) ----插入一條id=1,age=20的數據
insert into student values(2,"zhangsan",30) ----插入一條id=2,name="zhangsan",age=30的數據
4.修改數據
update 表名 set 字段=新值 where 修改條件
例子:update student set name="lisi",age=20 where _id=1 ----將id=1的這條數據的name字段的值修改為lisi,age字段的值修改為20
5.刪除數據
delete from 表名 where 刪除的條件
例子:
delete from student where _id=2 ----刪除student表中id=2的這條數據
delete from student ----刪除student表中所有的數據
6.查詢數據
select 字段名 from 表名 where 查詢條件 group by 分組的字段 having 篩選條件 order by 排序字段
例子:
select *from student ----查詢student表中的所有數據
select _id,name from student ----查詢student表中的_id和name這兩個字段
select *from student where _id=1 ----查詢student表中的_id=1的數據
select *from student where _id<>1 ----查詢student表中的_id不等于1的數據(<>代表不等于)
select *from student where _id=1 and age>18 ----查詢student表中的_id=1,age>18的數據
select *from student where name like "%小%" ----查詢student表中name字段中中間含有“小”字的數據,“小”字前面和后面可以是任意的字符
select *from student where name like "_小%" ----查詢student表中name字段中一個字符之后含有“小”字的數據,“小”字后面可以是任意的字符
select *from student where name is null ----查詢student表中name字段為null的數據
select *from student where age between 10 and 20 ----查詢student表中age字段的值為10-20的數據
select *from student where age>18 order by ----查詢student表中age的值大于18的數據,并根據_id進行排序
select * from student limit 1,20 ----查詢student表中第一頁的20條數據(數據庫數據分頁顯示的時候使用)
Android中SQLite的使用
1.繼承SQLiteOpenHelper類,并實現其中的方法
/**
* SQLiteOpenHelper
* 1.提供了onCreate() onUpgrade()等創建數據庫更新數據庫的方法
* 2.提供了獲取數據庫對象的函數
*/
public class MySqliteHple extends SQLiteOpenHelper{
public MySqliteHple(Context context) {
super(context, Constant.DATABASE_NAME, null, Constant.DATABASE_VERSION);
}
/**
* 構造函數
* @param context 上下文對象
* @param name 表示創建數據庫的名稱
* @param factory 游標工廠
* @param version 表示創建數據庫的版本 >=1
*/
public MySqliteHple(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
/**
* 當數據庫創建時回調的函數
* @param db 數據庫對象
*/
@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);//執行sql語句
}
/**
* 當數據庫版本更新時回調的函數
* @param db 數據庫對象
* @param oldVersion 數據庫舊版本
* @param newVersion 數據庫新版本
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i("tag","------onUpgrade-------");
}
/**
* 當數據庫打開時回調的函數
* @param db 數據庫對象
*/
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
Log.i("tag","------onOpen-------");
}
}
2.通過SQLiteOpenHelper獲取數據庫SQLiteDatabase對象
//getReadableDatabase() getWritableDatabase() 創建或打開數據庫,如果數據庫不存在則創建數據庫,如果數據庫
//存在則直接打開數據庫。默認情況下兩個函數都表示打開或者創建可讀可寫的數據庫對象,如果磁盤已滿或者數據庫本身權限等
//情況下getReadableDatabase()打開的是只讀數據庫
SQLiteDatabase db=mHple.getWritableDatabase();
3.增刪改查
使用sql語句操作:
//插入數據
String sql="insert into student values(1,'小白',30)";
db.execSQL(sql);
//更新數據
String sql="update set student name='小黑' where _id=1";
db.execSQL(sql);
//刪除數據
String sql="delete from student where _id=1";
db.execSQL(sql);
//查詢數據
String sql="select * from student";
cursor=db.rawQuery(sql,null);
使用Api進行操作:
插入數據
/**
* insert(String table, String nullColumnHack, ContentValues values)
*
* String table 表示插入數據表的名字
* String nullColumnHack SQL要求插入的數據不能全為null,但有些字段可以為null。一般這個參數我們直接給null
* ContentValues values 鍵為String類型的HashMap集合
* 返回值為long類型 表示插入數據的列數 如果值為-1則表示插入失敗
*/
insert(String table, String nullColumnHack, ContentValues values)
更新數據
/**
* update(String table, ContentValues values, String whereClause, String[] whereArgs)
*
* String table 表示修改數據表的名字
* ContentValues values 鍵為String類型的HashMap集合
* String whereClause 表示修改條件
* String[] whereArgs 表示修改條件的占位符
*/
update(String table, ContentValues values, String whereClause, String[] whereArgs)
刪除數據
/**
* delete(String table, String whereClause, String[] whereArgs)
* String table 表示刪除數據表的名字
* String whereClause 表示刪除條件
* String[] whereArgs 表示刪除條件的占位符
*/
delete(String table, String whereClause, String[] whereArgs)
查詢數據
/**
* 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.數據庫顯式開啟事務
db.beginTransaction();
2.提交當前事務
db.setTransactionSuccessful();
3.關閉事務
db.endTransaction();
例如我要插入大量數據的時候可以使用:
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數據庫分頁
當數據庫中含有大量數據時,如果一次性加載的程序中很容易造成程序崩潰、卡頓。為了提高用戶體驗,我們需將數據庫進行分頁操作。
//主要使用一下sql語句
select * from student limit ?,?
具體可以 查看demo:
demo地址:SQLite demo