我已经建立了一个数据库并对其进行了编辑。我想在我的Android应用程序中使用该数据库。 (API 24)所以我正在将数据库从资产复制到应用程序的数据
File f = getDatabasePath("sozluk.db");
if (!f.exists()) {
if (!f.getParentFile().exists()) {
f.getParentFile().mkdir();
}
}
try {
InputStream is = getAssets().open("sozluk_k.db");
OutputStream os = new FileOutputStream(f);
byte[] buffer = new byte[1024];
int b;
while ((b = is.read(buffer)) > 0) {
os.write(buffer, 0, b);
}
os.flush();
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
文件已成功复制,因为我可以通过其他应用程序打开它,但是此代码给出了错误
SQLiteDatabase db = SQLiteDatabase.openDatabase("sozluk.db",null,MODE_PRIVATE);
2018-12-07 22:07:31.297 24549-24549/com.yaksit.ebced W/SQLiteLog: (28) failed to open "//sozluk.db" with flag (131074) and mode_t (0) due to error (2)
2018-12-07 22:07:31.298 24549-24549/com.yaksit.ebced W/SQLiteLog: (28) failed to open "//sozluk.db" with flag (131072) and mode_t (0) due to error (2)
2018-12-07 22:07:31.298 24549-24549/com.yaksit.ebced E/SQLiteLog: (14) cannot open file at line 32887 of [69906880ce]
2018-12-07 22:07:31.298 24549-24549/com.yaksit.ebced E/SQLiteLog: (14) os_unix.c:32887: (2) open(//sozluk.db) -
2018-12-07 22:07:31.298 24549-24549/com.yaksit.ebced E/SQLiteLog: (1) Process om.yaksit.ebced : Pid (24549) Uid (10416) Euid (10416) Gid (10416) Egid (10416)
2018-12-07 22:07:31.298 24549-24549/com.yaksit.ebced E/SQLiteLog: (1) osStat failed "//sozluk.db" due to error (2)
2018-12-07 22:07:31.298 24549-24549/com.yaksit.ebced E/SQLiteLog: (1) Stat of / : st_mode(41777) st_uid(0) st_gid(0) st_ino(2)
2018-12-07 22:07:31.306 24549-24549/com.yaksit.ebced E/SQLiteDatabase: Failed to open database 'sozluk.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 1294): Could not open database
#################################################################
Error Code : 1294 (SQLITE_CANTOPEN_ENOENT)
Caused By : Specified directory or database file does not exist.
(unknown error (code 1294): Could not open database)
#################################################################
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:242)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:203)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:518)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:209)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:181)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:1156)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:1101)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:832)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:807)
at com.yaksit.ebced.MainActivity.oku(MainActivity.java:38)
at com.yaksit.ebced.MainActivity.access$100(MainActivity.java:26)
at com.yaksit.ebced.MainActivity$1.onClick(MainActivity.java:130)
at android.view.View.performClick(View.java:6261)
at android.widget.TextView.performClick(TextView.java:11185)
at android.view.View$PerformClick.run(View.java:23752)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
我通过f.getAbsolutePath()
检查了它是否存在,并返回了/data/user/0/com.yaksit.ebced/databases/sozluk.db
,因此文件是否以某种方式损坏了?
答案 0 :(得分:0)
将SQLiteDatabase db = SQLiteDatabase.openDatabase("sozluk.db",null,MODE_PRIVATE);
更改为SQLiteDatabase db = openOrCreateDatabase("sozluk.db", MODE_PRIVATE, null);
是可行的,但是为什么呢?