- 綠色版查看
- 綠色版查看
- 綠色版查看
- 綠色版查看
SQLiteStudio 是一個跨平臺的 SQLite 數(shù)據(jù)庫的管理工具,采用 Tcl 語言開發(fā)。
SQLiteStudio的特色:
功能完善的sqlite2和sqlite3工具,視圖編碼支持utf8。
支持導(dǎo)出數(shù)據(jù)格式:csv、html、plain、sql、xml,
可同時打開多個數(shù)據(jù)庫文件
支持查看和編輯二進(jìn)制字段
SqliteStudio怎么用?
使用“sqlitestudio”打開(也可以使用其它sqlite可視化工具,個人習(xí)慣使用該工具,多語言小巧無需安裝)
1、打開sqlitestudio ”數(shù)據(jù)庫“-”添加數(shù)據(jù)庫“
2、選擇導(dǎo)出的那個數(shù)據(jù)庫文件直接打開即可。
3、在sqlitestudio 可以創(chuàng)建和修改表也可以執(zhí)行SQL語句基本可以滿足常用需求
在sqlitestudio 導(dǎo)出表數(shù)據(jù)直接右鍵”導(dǎo)出表“ 默認(rèn)格式CSV文件
4、導(dǎo)入表數(shù)據(jù):
右鍵表名“import data to table”
SqliteStudio在Android程序中使用已有的SQLite數(shù)據(jù)庫
1. 準(zhǔn)備SQLite database文件
假設(shè)你已經(jīng)創(chuàng)建了一個sqlite數(shù)據(jù)庫,我們需要對其進(jìn)行一些修改。
(譯者注:這里原文是推薦了一個SQLite數(shù)據(jù)庫管理軟件,這個我覺得可以隨自己的喜好,最Windows下面有多款可視化的SQlite數(shù)據(jù)庫管理軟件,可以方便的讀取,編輯數(shù)據(jù)庫,例如我用的是sqlitestudio
打開數(shù)據(jù)庫,添加一個新的table “android_metadata",插入一行數(shù)據(jù),具體的SQL如下:
[sql] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')
INSERT INTO "android_metadata" VALUES ('en_US')
(譯者注:上面兩行是表明需要進(jìn)行的操作,具體可以直接在sqlitesstudio中完成)
然后你需要對你數(shù)據(jù)表格的primary id 列重命名為 “_id”,這樣Adroid會知道怎么對id列進(jìn)行綁定,你可以很容易的在SQlite數(shù)據(jù)庫管理軟件中進(jìn)行列編輯。
這兩步之后,你的sqlite數(shù)據(jù)庫文件就準(zhǔn)備好了。
(譯者注:這里我保留了id列,即沒有對其進(jìn)行重命名,測試證明也是沒有問題的)
2. 在你的Android程序中復(fù)制,打開以及訪問數(shù)據(jù)庫
現(xiàn)在把你上一步準(zhǔn)備好的數(shù)據(jù)庫文件放在“assets”文件夾下面,然后通過繼承 SQLiteOpenHelper類來創(chuàng)建一個Database Helper類,
你的DataBaseHelper類大致可以如下:
public class DataBaseHelper extends SQLiteOpenHelper{
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
private static String DB_NAME = "myDBName";
private SQLiteDatabase myDataBase;
private final Context myContext;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.
}
就這樣。
現(xiàn)在你可以創(chuàng)建一個新的DataBaseHelper實例,然后調(diào)用createDataBase(),然后再調(diào)用openDataBase()方法,記住修改DB_PATH字符串中“YOUR_PACKAGE”為你真正的package名稱(也就是說com.examplename.myapp)
以下是示范代碼:
[java] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
...
DataBaseHelper myDbHelper = new DataBaseHelper();
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
...
您的評論需要經(jīng)過審核才能顯示
有用
有用
有用