gpt4 book ai didi

java - 如何在android中将数据库文件附加到apk文件

转载 作者:行者123 更新时间:2023-12-01 13:03:04 25 4
gpt4 key购买 nike

我有一个数据库,其中大约有 6 个表。我想在手机上部署我的应用程序。但是如果我创建一个 apk 文件并将其加载到手机上,我如何将数据库文件附加到它?请帮助

编辑:我按照在 Assets 文件夹中加载数据库的过程,并更改 sqliteopenhelper 类,但应用程序仍然在手机上关闭

下面是我的 sqliteopenhelper

public class openingclass extends SQLiteOpenHelper
{
public openingclass(Context c) {
super(c,Db_NAME, null, DB_VERSION);
}
public void createDataBase() {

boolean dbExist;
try {

dbExist = checkDataBase();


} catch (SQLiteException e) {

e.printStackTrace();
throw new Error("database dose not exist");

}

if(dbExist){
//do nothing - database already exist
}else{

try {

copyDataBase();


} catch (IOException e) {

e.printStackTrace();
throw new Error("Error copying database");

}
//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();


}

}

/**
* 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.
throw new Error("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{



//copyDataBase();
//Open your local db as the input stream
InputStream myInput = c1.getAssets().open(Db_NAME);

// Path to the just created empty db
String outFileName = DB_PATH +"/"+ Db_NAME;
File databaseFile = new File( DB_PATH);
// check if databases folder exists, if not create one and its subfolders
if (!databaseFile.exists()){
databaseFile.mkdir();
}

//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();



}



@Override
public synchronized void close() {

if(myDataBase != null)
myDataBase.close();

super.close();

}

@Override
public void onCreate(SQLiteDatabase arg0) {
String S = "create table " +
TABLE_NAME +
" (" +
TABLE_COL_MAIL + " text primary key," +
TABLE_COL_NAME + " text," +
TABLE_COL_PASS + " text," +
TABLE_COL_PHO + " text," +
TABLE_COL_ADD + " text," +
TABLE_COL_GEN + " text," +
TABLE_COL_DOB + " text" +
");";
arg0.execSQL(S);

String S1 = "create table " +
SECOND_TABLE_NAME +
" (" +
TABLE_COL_USER + " text," +
TABLE_COL_PRODUCT + " text," +
TABLE_COL_QUANTITY + " integer" +
");";
arg0.execSQL(S1);




}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}

}

最佳答案

您可以在 assets/ 文件夹中包含一个文件。在 SQLiteOpenHelper onCreate() 方法中,您可以打开文件并从中读取内容,然后使用它来创建数据库。

关于java - 如何在android中将数据库文件附加到apk文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23407390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com