gpt4 book ai didi

android - 在 SQLite 数据库 Android Studio 上使用预填充数据库

转载 作者:搜寻专家 更新时间:2023-10-30 20:37:37 24 4
gpt4 key购买 nike

<分区>

在这里,我想将我预先填充的 SQLite 数据库用于我的 Android 应用程序。因此,当第一次运行时,它会自动从 assets 文件夹复制数据库并将其用作我的 android 应用程序上的数据库。

到目前为止,我已经在做这些事情了:

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

}

但是我收到这样的错误信息:

10-28 07:18:38.988 2425-2425/gook.psikologikarakter E/SQLiteLog﹕ (14) cannot open file at line 30046 of [9491ba7d73] 10-28 07:18:38.988 2425-2425/gook.psikologikarakter E/SQLiteLog﹕ (14) os_unix.c:30046: (2) open(/Users/ever_ncn/Documents/Project/PsikologiKarakter/app/src/main/assets/question.db) - 10-28 07:18:38.989 2425-2425/gook.psikologikarakter E/SQLiteDatabase﹕ Failed to open database '/Users/ever_ncn/Documents/Project/PsikologiKarakter/app/src/main/assets/question.db'. android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method) at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209) at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193) at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463) at 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.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791) at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694) at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669) at gook.psikologikarakter.DatabaseHelper.DBExist(DatabaseHelper.java:65) at gook.psikologikarakter.DatabaseHelper.createDB(DatabaseHelper.java:52) at gook.psikologikarakter.DatabaseHelper.createDatabase(DatabaseHelper.java:48) at 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.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.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(ZygoteInit.java:698)

我不知道到底发生了什么,但我想我无法打开数据库文件。但我不知道该怎么办。请高手帮帮我。

先谢谢了。

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