這裏我想使用預填充的SQLite數據庫到我的android應用程序。所以,第一次運行時,它會自動從assets文件夾中複製數據庫,並將其用作我的android應用程序中的數據庫。在SQLite數據庫上使用預填充的數據庫Android Studio
到目前爲止我做這個東西:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String MyVillageSoftware = "MyVillageSoftware";
public static final String table_Question = "table_question";
private static final String DATABASE_PATH="/Users/ever_ncn/Documents/Project/PsikologiKarakter/app/src/main/assets/";
private static final String DATABASE_NAME="question.db";
//TOL for transaction Coloumn
SQLiteDatabase db;
Context context;
private static final String TAG = DatabaseHelper.class.getSimpleName();
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
public void createDatabase(){
createDB();
}
public void createDB(){
boolean dbExist = DBExist();
if(!dbExist){
this.getReadableDatabase();
copyDBFromResource();
}
}
private boolean DBExist(){
SQLiteDatabase db = null;
try{
String databasePath = DATABASE_PATH+DATABASE_NAME;
db=SQLiteDatabase.openDatabase(databasePath, null,
SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
}catch(SQLiteException e){
Log.e("SqlHelper", "Database nda ada babi");
}
if(db != null){
db.close();
}return db != null ? true : false;
}
private void copyDBFromResource(){
InputStream inStream=null;
OutputStream outStream = null;
String dbFilePath=DATABASE_PATH+DATABASE_NAME;
try{
inStream=context.getAssets().open(DATABASE_NAME);
outStream=new FileOutputStream(dbFilePath);
byte[] buffer = new byte[1024];
int length;
while((length=inStream.read(buffer))>0){
outStream.write(buffer, 0 , length);
}
outStream.flush();
outStream.close();
inStream.close();
}catch (IOException e){
throw new Error("Problem cuk");
}
}
public List<String> getAllCategory() {
List<String> AllCategoryList = new ArrayList<String>();
List<String> AllCategIdList = new ArrayList<String>();
String selectQuery = "SELECT * FROM " + table_Question;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
String Id = cursor.getString(0);
String A = cursor.getString(1);
String B = cursor.getString(2);
String C = cursor.getString(3);
String D = cursor.getString(4);
String aExplain = cursor.getString(5);
String bExplain = cursor.getString(6);
String cExplain = cursor.getString(7);
String dExplain = cursor.getString(8);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return AllCategoryList;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + table_Question);
onCreate(db);
}
}
但我得到了這樣的錯誤消息:
7月10日至28日:18:38.988 2425年至2425年/gook.psikologikarakter E/SQLiteLog:(14)無法在[9491ba7d73]10-28 07:18:38.988 2425-2425/gook.psikologikarakter E/SQLiteLog:(14)os_unix.c:30046: (2)打開(/ Users/ever_ncn/Documents/Project/Psikolog iKarakter/app/src/main/assets/question.db) - 10-28 07:18:38.989 2425-2425/gook.psikologikarakter E/SQLiteDatabase:未能打開數據庫'/ Users/ever_ncn/Documents/Project/PsikologiKarakter /app/src/main/assets/question.db」。 android.database.sqlite.SQLiteCantOpenDatabaseException:未知錯誤(代碼14):無法打開數據庫 at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method) at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java :209) 在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193) 在android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463) 在android.database.sqlite.SQLiteConnectionPool.open (SQLiteConnectionPool.java:185) at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177) at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806) at android.dat在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694) (位於android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669))上的基於SQLite數據庫的SQLiteDatabase.open(SQLiteDatabase.java:791) 在gook.psikologikarakter.DatabaseHelper.DBExist(DatabaseHelper.java:65) 在gook.psikologikarakter.DatabaseHelper.createDB(DatabaseHelper.java:52) 在gook.psikologikarakter.DatabaseHelper.createDatabase(DatabaseHelper.java:48) 在gook.psikologikarakter.MainActivity.onCreate(MainActivity.java:59) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android。 app.ActivityThread.per在android.app.ActivityThread.access $ 800(ActivityThread.java:151) at android.app.ActivityThread $ H上創建了一個formLaunchActivity(ActivityThread.java:2278) (android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) ) .handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main (ActivityThread.java:5257) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os。 ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(Zy goteInit.java:698)
我不知道真的發生了什麼,但我認爲我無法打開數據庫文件。但我不知道該怎麼做。請高手幫幫我。
以前感謝。
您無法使用資產文件夾中的數據庫。您需要在第一次運行時將數據庫複製到/ data/data/YOUR_PACKAGE/databases /中。這將是幫助:http://stackoverflow.com/questions/513084/how-to-ship-an-android-application-with-a-database – ChallengeAccepted
是的先生,我的意思是,所以我的問題是如何複製數據庫從assets文件夾到data/data/your_package/databases /當應用程序第一次啓動時? –
我的朋友就像複製任何其他文件一樣簡單,你會使用java。這裏是一個特別爲你的情況:http://stackoverflow.com/a/20592481/1371041 – ChallengeAccepted