標籤:
SQLite簡介
Google為Andriod的較大的資料處理提供了SQLite,他在資料存放區、管理、維護等各方面都相當出色,功能也非常的強大。SQLite具備下列特點:
1.輕量級
使用 SQLite 只需要帶一個動態庫,就可以享受它的全部功能,而且那個動態庫的尺寸想當小。
2.獨立性
SQLite 資料庫的核心引擎不需要依賴第三方軟體,也不需要所謂的“安裝”。
3.隔離性
SQLite 資料庫中所有的資訊(比如表、視圖、觸發器等)都包含在一個檔案夾內,方便管理和維護。
4.跨平台
SQLite 目前支援大部分作業系統,不至電腦作業系統更在眾多的手機系統也是能夠運行,比如:Android。
5.多語言介面
SQLite 資料庫支援多語言編程介面。
6.安全性
SQLite 資料庫通過資料庫級上的獨佔性和共用鎖定來實現獨立交易處理。這意味著多個進程可以在同一時間從同一資料庫讀取資料,但只能有一個可以寫入資料。
Android中的SQLite使用
首先建立資料庫類
public class DatabaseHelper extends SQLiteOpenHelper { private static final String DB_NAME = "mydata.db" ; //資料庫名稱 private static final int version = 1 ; //資料庫版本 public DatabaseHelper(Context context) { super (context, DB_NAME, null , version); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { String sql = "create table user(username varchar(20) not null , password varchar(60) not null );" ; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } } |
SQLiteOpenHelper類介紹
SQLiteOpenHelper是SQLiteDatabase的一個協助類,用來管理資料庫的建立和版本的更新。一般是建立一個類繼承它,並實現它的onCreate和onUpgrade方法。
方法名 |
方法描述 |
SQLiteOpenHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version) |
構造方法,一般是傳遞一個要建立的資料庫名稱那麼參數 |
onCreate(SQLiteDatabase db) |
建立資料庫時調用 |
onUpgrade(SQLiteDatabase db,int oldVersion , int newVersion) |
版本更新時調用 |
getReadableDatabase() |
建立或開啟一個唯讀資料庫 |
getWritableDatabase() |
建立或開啟一個讀寫資料庫 |
下面來介紹調用的方法
建立資料庫
這裡特別的地方是通過調用了SQLiteOpenHelper類的getReadableDatabase()方法來實現建立一個資料庫的
123 |
DatabaseHelper database = new DatabaseHelper( this ); //這段代碼放到Activity類中才用this SQLiteDatabase db = null ; db = database.getReadalbeDatabase(); |
SQLiteDatabase類為我們提供了很多種方法,而較常用的方法如下
(傳回值)方法名 |
方法描述 |
(int) delete(String table,String whereClause,String[] whereArgs) |
刪除資料行的便捷方法 |
(long) insert(String table,String nullColumnHack,ContentValues values) |
添加資料行的便捷方法 |
(int) update(String table, ContentValues values, String whereClause, String[] whereArgs) |
更新資料行的便捷方法 |
(void) execSQL(String sql) |
執行一個SQL語句,可以是一個select或其他的sql語句 |
(void) close() |
關閉資料庫 |
(Cursor) query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) |
查詢指定的資料表返回一個帶遊標的資料集 |
(Cursor) rawQuery(String sql, String[] selectionArgs) |
運行一個預置的SQL語句,返回帶遊標的資料集(與上面的語句最大的區別就是防止SQL注入) |
資料的添刪改查分別可以通過2種途徑來實現
資料的添加
1.使用insert方法
123 |
ContentValues cv = new ContentValues(); //執行個體化一個ContentValues用來裝載待插入的資料cv.put("username","Jack Johnson");//添加使用者名稱 cv.put( "password" , "iLovePopMusic" ); //添加密碼 db.insert( "user" , null ,cv); //執行插入操作 |
2.使用execSQL方式來實現
12 |
String sql = "insert into user(username,password) values ( ‘Jack Johnson‘ , ‘iLovePopMuisc‘ ); //插入操作的SQL語句 db.execSQL(sql); //執行SQL語句 |
資料的刪除
同樣有2種方式可以實現
123 |
String whereClause = "username=?" ; //刪除的條件 String[] whereArgs = { "Jack Johnson" }; //刪除的條件參數 db.delete( "user" ,whereClause,whereArgs); //執行刪除 |
使用execSQL方式的實現
12 |
String sql = "delete from user where username=‘Jack Johnson‘" ; //刪除操作的SQL語句 db.execSQL(sql); //執行刪除操作 |
資料修改
同上,仍是2種方式
12345 |
ContentValues cv = new ContentValues(); //執行個體化ContentValues cv.put( "password" , "iHatePopMusic" ); //添加要更改的欄位及內容 String whereClause = "username=?" ; //修改條件 String[] whereArgs = { "Jack Johnson" }; //修改條件的參數 db.update( "user" ,cv,whereClause,whereArgs); //執行修改 |
使用execSQL方式的實現
12 |
String sql = "update [user] set password = ‘iHatePopMusic‘ where username=‘Jack Johnson‘" ; //修改的SQL語句 db.execSQL(sql); //執行修改 |
資料查詢
資料查詢相對前面幾種方法就複雜一些了,因為查詢會帶有很多條件
通過query實現查詢的
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
各參數說明:
- table:表名稱
- colums:列名稱數組
- selection:條件子句,相當於where
- selectionArgs:條件陳述式的參數數組
- groupBy:分組
- having:分組條件
- orderBy:排序類
- limit:分頁查詢的限制
- Cursor:傳回值,相當於結果集ResultSet
針對遊標(Cursor)也提供了不少方法
方法名稱 |
方法描述 |
getCount() |
總記錄條數 |
isFirst() |
判斷是否第一條記錄 |
isLast() |
判斷是否最後一條記錄 |
moveToFirst() |
移動到第一條記錄 |
moveToLast() |
移動到最後一條記錄 |
move(int offset) |
移動到指定的記錄 |
moveToNext() |
移動到嚇一條記錄 |
moveToPrevious() |
移動到上一條記錄 |
getColumnIndex(String columnName) |
獲得指定列索引的int類型值 |
實現代碼
12345678 |
Cursor c = db.query( "user" , null , null , null , null , null , null ); //查詢並獲得遊標 if (c.moveToFirst()){ //判斷遊標是否為空白 for ( int i= 0 ;i<c.getCount();i++){ c.move(i); //移動到指定記錄 String username = c.getString(c.getColumnIndex( "username" ); String password = c.getString(c.getColumnIndex( "password" )); } } |
通過rawQuery實現的帶參數查詢
1234 |
Cursor c = db.rawQuery( "select * from user where username=?" , new Stirng[]{ "Jack Johnson" }); if (cursor.moveToFirst()) { String password = c.getString(c.getColumnIndex( "password" )); } |
andriod sqlite 詳解轉載