我在移動設備上直接開發源碼文件,我有一些問題,你希望能幫助我使用一個數據庫,澄清有關發展:在哪裏放置在一個Android應用程序
我的應用程序是意味着使用SQLite數據庫(只讀模式),所以我想 知道我應該在電腦上創建.sqlite文件的位置,在 命令應用程序讀取,甚至直接。
後來,應用程序應該從 集中式服務器下載數據庫文件。因此,我們的想法是繼續將該文件放在 的相同位置,其中一個應用程序一定會知道,並且可以在嘗試使用它之前檢查它是否存在 。
謝謝!
我在移動設備上直接開發源碼文件,我有一些問題,你希望能幫助我使用一個數據庫,澄清有關發展:在哪裏放置在一個Android應用程序
我的應用程序是意味着使用SQLite數據庫(只讀模式),所以我想 知道我應該在電腦上創建.sqlite文件的位置,在 命令應用程序讀取,甚至直接。
後來,應用程序應該從 集中式服務器下載數據庫文件。因此,我們的想法是繼續將該文件放在 的相同位置,其中一個應用程序一定會知道,並且可以在嘗試使用它之前檢查它是否存在 。
謝謝!
您需要將SQLite數據庫打包爲應用程序中的資源(例如res/raw)或資產。但是,您將無法直接使用打包的SQLite數據庫。您需要將其複製到Android預計應用程序的數據庫文件所在的位置,然後從那裏使用它。執行此操作的最佳方法是使用Context.getDatabasePath(String)
(傳遞數據庫文件的名稱)來確定文件路徑。這也是您應該放置應用程序在運行時下載的數據庫文件的位置。
您需要使用下面的代碼將數據庫文件複製到你的資產文件夾,然後將其寫入內存訪問的應用程序:
public class DataBaseHelper extends SQLiteOpenHelper{
private static final String TAG = "DataBaseHelper";
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/" + Constants.package_name + "/databases/";
private static String DB_NAME = "YourDatabaseName.db";
public 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.getWritableDatabase();
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_READWRITE);
}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[2048];
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_READWRITE);
}
@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) {
}
希望這有助於。
在你的DataBaseHelper類中,你應該使用'myContext.getDatabasePath(「YourDatabaseName」)'而不是對'DB_PATH'進行硬編碼。 – 2012-04-09 14:15:56
我在生產應用程序中有類似於此的代碼一段時間,雖然它似乎大部分時間工作,但我懷疑在複製過程中,數據庫在這裏和那裏幾乎被損壞或更改。我注意到缺少列和損壞的表索引。 – 2012-04-09 14:17:44
非常感謝代碼,我會嘗試做一些修改。 – giorgiline 2012-04-10 11:52:45
我把我的初始數據庫放在'assets'目錄下,然後將它複製到用戶可訪問的路徑上 - 比如外部內存(sdcard等)。我沒有用這種方式:)任何麻煩
如果要插入數據,以及寧願只是獲取值 可以使應用程序
將始終具有路徑數據庫裏面我建議
像
/數據/數據/ your_project_name /數據庫/ yourdatabase_name
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/
這是非常好的教程,謝謝你讓我知道。 – giorgiline 2012-04-10 11:51:55
對於路徑,應該使用'your_project_package'(在清單中聲明),而不是'your_project_name'。無論如何,確定路徑的最好方法是使用'context「。getDatabasePath( 「yourdatabase_name」)'。 – 2012-04-10 14:08:57
感謝您的回答。現在我已經清楚它是如何完成的。 – giorgiline 2012-04-10 11:51:13