Android-使用嵌入式SQLite

來源:互聯網
上載者:User

Android平台上整合的一個嵌入式關係型資料庫,SQLite3支援 NULL,INTEGER,REAL(浮點數

字),TEXT(字串文本)和BLOB(二進位對象)資料類型,雖然它支援的類型只有五種,但實際上

sqlite3也接受varchar(n),char(n),decimal(p,s) 等資料類型,只不過在運算或儲存時會轉

成對應的五種資料類型.

SQLite最大的特點是你可以把各種類型的資料儲存到任何欄位中,而不用關心欄位聲明的資料類型是

什麼. 但定義為INTEGER PRIMARY KEY的欄位只能儲存64位整數, 當向這種欄位儲存除整數以外

的資料時,將會產生錯誤.

另外, SQLite在解析CREATE TABLE語句時,會忽略欄位名後面的資料類型資訊.

SQLite可以解析大部分標準SQL語句,如:

查詢語句:select * from .. where group by ..having ... order by 排序子句

SQLite分頁同mysql相同:

... limit 5 offset 3 | limit 3,5

首次使用資料庫需要建立表及初始化一些資訊,升級時需要修改表資訊,android提供了SQLiteOpenHelper完成此類功能

onCreate(SQLiteDatabase db)方法用於首次使用時建立庫,onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)方法檢測版本變化時更新庫

getWriteableDatabase()和getReadableDatabase()方法擷取一個用於操作資料庫的SQLiteDatabase執行個體。

getWriteableDatabase()方法一讀寫方式開啟資料庫,一旦資料庫的磁碟空間滿了,資料庫只能讀而不能寫。倘若使用getWriteableDatabase()開啟資料庫就會出錯。getReadableDatabase()方法以讀方式開啟資料庫。

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String name = "itcast";//資料庫名稱

private static final int version = 1;      //資料庫版本

public DatabaseHelper(Context context){

Super(context,name,null,version);

}

public void onCreate(){

//drop table if exists customers

//create table if not exists customers(...

db.execSQL("CREATE TABLE IF NOT EXISTS person (personid integer  primary                           key autoincrement, name varchar(20), age INTEGER)");

}

public void onUpgrade(SQLiteDatabase db,int oldVersion,intnewVersion){

db.execSQL("  ALTER TABLE person ADD phone VARCHAR(12)  NULL");

}

}

在實際項目開發中,當資料庫表結構發生更新時,應該避免使用者存放於資料庫中的資料丟失。

使用SQLiteDataBase資料庫

SQLiteDatabase db = ....;

db.execSQL("insert into person(name,age) values(?,?)",new Object[]{..});

db.close();

//查詢操作

Cursor cursor = db.rawQuery("select * from person", null);

while(cursor.moveToNext){

int personid = cursor.getInt(0);

String name = cursor.getString(1);

int age = cursor.getInt(2);

cursor.close();

Db.close();

}

cursor.close();

db.close();

SQLiteDatabase db = databaseHelper.getWritableDatabase();

ContentValues values = new ContentValues();

Values.put("name","傳智播客");

.....

Long rowid = db.insert("person",null,values);

//刪除

SQLiteDatabase db = databaseHelper.getWritableDatabase();

db.delete("person","personid<?",new String[]{"2"}">");

db.close();

//更新

SQLiteDatabase db = databaseHelper.getWritableDatabase();

ContentValues values = new ContentValues();

//key為欄位名,value為值

values.put("name","傳智播客");

db.update("person",values,"personid=?",new String[]{"1"});

db.close();

註:第一次調用getWritableDatabase()或getReadableDatabase()方法後,SQLiteOpenHelper

會緩衝當前的SQLiteDatabase執行個體,SQLiteDatabase執行個體正常情況下會鑑效組資料庫的開啟狀態,所以在你不需要SQLiteDatabase執行個體時,請及時調用close()方法釋放資源,一旦SQLiteDatabase執行個體被緩衝,多次調用getWritableDatabase()或getReadableDatabase()方法得到的都是同一執行個體。

交易處理:

SQLiteDatabase db = ...;

db.beginTransaction();

Try{

db.execSQL(...)

db.execSQL(...);

//設定成功標記

db.setTransactionSuccessful();

}catch(){

....

} finally {

db.endTransaction();//由事務的標誌決定是提交事務,還是復原事務。

}

db.close(); www.2cto.com

注意:建立表時,只能用integer類型,不能用int作為主鍵,否則不支援autoincrement.

create table customers(id integer primary key autoincrement,name text);


作者:toto1297488504

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.