由于android内置了SQLite資料庫!
SQLite本身就是一個很小型的資料庫!
資料庫存儲的位置在data/data/<項目檔案夾>/databases/
1:建立資料庫Context.createDatabase(String name,int version ,int mode,CursorFactory factory)
如果建立不成功則抛出FileNotFoundException異常
Java代碼
SQLiteDatabase DataBase=this.openOrCreateDatabase("DataBase.db",
MODE_PRIVATE,
new CursorFactory(){
//建立新的資料庫,名稱Database,模式MODE_PRIVATE
public Cursor newCursor(SQLiteDatabase db,
SQLiteCursorDriver masterQuery,
String editTable,
SQLiteQuery query){
return null;
}
});
2:打開已經存在的資料庫Context.openDatabase(String file,CursorFactory factory);
如果我們要實作打開不存在的資料庫就建立它,代碼如下:
//建立一個名為:DataBase的資料庫,字尾為.db
SQLiteDatabase DataBase=this.openOrCreateDatabase("DateBase.db",
MODE_PRIVATE,
null);
//也可以這樣寫
try
{
//打開已經存在的資料庫
db = this.openDatabase("DateBase.db", null);
}
catch (FileNotFoundException e)
try
{
//建立新的資料庫
}
catch (FileNotFoundException e)
db = null;
}
3:關閉資料庫
//千萬不要忘記關閉資料庫哦
DataBase.close();
4:删除指定資料庫
this.deleteDatabase("Database.db");
5:執行SQL指令使用:SQLiteDatabase.execSQL(String sql);