- 綠色版查看
- 綠色版查看
- 綠色版查看
- 綠色版查看
SQLiteStudio 是一個(gè)跨平臺(tái)的 SQLite 數(shù)據(jù)庫(kù)的管理工具,采用 Tcl 語(yǔ)言開(kāi)發(fā)。
SQLiteStudio的特色:
功能完善的sqlite2和sqlite3工具,視圖編碼支持utf8。
支持導(dǎo)出數(shù)據(jù)格式:csv、html、plain、sql、xml,
可同時(shí)打開(kāi)多個(gè)數(shù)據(jù)庫(kù)文件
支持查看和編輯二進(jìn)制字段
SqliteStudio怎么用?
使用“sqlitestudio”打開(kāi)(也可以使用其它sqlite可視化工具,個(gè)人習(xí)慣使用該工具,多語(yǔ)言小巧無(wú)需安裝)
1、打開(kāi)sqlitestudio ”數(shù)據(jù)庫(kù)“-”添加數(shù)據(jù)庫(kù)“
2、選擇導(dǎo)出的那個(gè)數(shù)據(jù)庫(kù)文件直接打開(kāi)即可。
3、在sqlitestudio 可以創(chuàng)建和修改表也可以執(zhí)行SQL語(yǔ)句基本可以滿足常用需求
在sqlitestudio 導(dǎo)出表數(shù)據(jù)直接右鍵”導(dǎo)出表“ 默認(rèn)格式CSV文件
4、導(dǎo)入表數(shù)據(jù):
右鍵表名“import data to table”
SqliteStudio在Android程序中使用已有的SQLite數(shù)據(jù)庫(kù)
1. 準(zhǔn)備SQLite database文件
假設(shè)你已經(jīng)創(chuàng)建了一個(gè)sqlite數(shù)據(jù)庫(kù),我們需要對(duì)其進(jìn)行一些修改。
(譯者注:這里原文是推薦了一個(gè)SQLite數(shù)據(jù)庫(kù)管理軟件,這個(gè)我覺(jué)得可以隨自己的喜好,最Windows下面有多款可視化的SQlite數(shù)據(jù)庫(kù)管理軟件,可以方便的讀取,編輯數(shù)據(jù)庫(kù),例如我用的是sqlitestudio
打開(kāi)數(shù)據(jù)庫(kù),添加一個(gè)新的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中完成)
然后你需要對(duì)你數(shù)據(jù)表格的primary id 列重命名為 “_id”,這樣Adroid會(huì)知道怎么對(duì)id列進(jìn)行綁定,你可以很容易的在SQlite數(shù)據(jù)庫(kù)管理軟件中進(jìn)行列編輯。
這兩步之后,你的sqlite數(shù)據(jù)庫(kù)文件就準(zhǔn)備好了。
(譯者注:這里我保留了id列,即沒(méi)有對(duì)其進(jìn)行重命名,測(cè)試證明也是沒(méi)有問(wèn)題的)
2. 在你的Android程序中復(fù)制,打開(kāi)以及訪問(wèn)數(shù)據(jù)庫(kù)
現(xiàn)在把你上一步準(zhǔn)備好的數(shù)據(jù)庫(kù)文件放在“assets”文件夾下面,然后通過(guò)繼承 SQLiteOpenHelper類來(lái)創(chuàng)建一個(gè)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)建一個(gè)新的DataBaseHelper實(shí)例,然后調(diào)用createDataBase(),然后再調(diào)用openDataBase()方法,記住修改DB_PATH字符串中“YOUR_PACKAGE”為你真正的package名稱(也就是說(shuō)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;
}
...
您的評(píng)論需要經(jīng)過(guò)審核才能顯示
有用
有用
有用