Java 代码14:无法打开数据库
我知道以前有人问过这个问题。但是,问题是,相同的代码(用于数据库处理程序)正在为另一个应用程序工作,但不是我当前正在开发的应用程序。我甚至通过检查“设置”中的权限来确保授予了权限。下面是日志: 05-13 15:35:45.693 29696-29696/com.example.hack.corrector E/SQLiteLog:(14)无法打开[5a3022e081]第31282行的文件 (14) os_unix.c:31282:(21)打开(/data/user/0/com.example.hack.corrector/databases/)- 05-13 15:35:45.694 29696-29696/com.example.hack.corrector E/SQLiteDatabase:未能打开数据库“/data/user/0/com.example.hack.corrector/databases/”。 android.database.sqlite.SQLiteCantoPendDatabaseException:未知错误(代码14):无法打开数据库 位于android.database.sqlite.SQLiteConnection.nativeOpen(本机方法) 位于android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:207) 位于android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191) 位于android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463) 位于android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185) 位于android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177) 位于android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806) 位于android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791) 位于android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694) 位于android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669) 位于com.example.habc.corrector.VocabDatabase.openDataBase(VocabDatabase.java:127) 位于com.example.habc.corrector.scrapeservice.createDB(scrapeservice.java:31) 位于com.example.habc.corrector.scrapeservice.onStartCommand(scrapeservice.java:23) 位于android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3049) 在android.app.ActivityThread.access$2300(ActivityThread.java:154) 在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1479)上 位于android.os.Handler.dispatchMessage(Handler.java:102) 位于android.os.Looper.loop(Looper.java:157) 位于android.app.ActivityThread.main(ActivityThread.java:5571) 位于java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)上 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635) 下面是数据库处理程序代码:Java 代码14:无法打开数据库,java,android,database,sqlite,sqliteopenhelper,Java,Android,Database,Sqlite,Sqliteopenhelper,我知道以前有人问过这个问题。但是,问题是,相同的代码(用于数据库处理程序)正在为另一个应用程序工作,但不是我当前正在开发的应用程序。我甚至通过检查“设置”中的权限来确保授予了权限。下面是日志: 05-13 15:35:45.693 29696-29696/com.example.hack.corrector E/SQLiteLog:(14)无法打开[5a3022e081]第31282行的文件 (14) os_unix.c:31282:(21)打开(/data/user/0/com.example
package com.example.hack.corrector;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class VocabDatabase extends SQLiteOpenHelper {
//The Android's default system path of your application database.
private static String DB_PATH = "";
private static String DB_NAME = "ztr.db";
private 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 VocabDatabase(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
this.DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
}
/**
* 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() {
this.getReadableDatabase();
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
e.printStackTrace();
}
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[1024];
int length = 0;
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;
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) {
if (newVersion > oldVersion) {
try {
copyDataBase();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.
//add your public methods for insert, get, delete and update data in database.
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
SQLiteDatabase db = this.getWritableDatabase();
return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
}
public long insert(String table, String nullColumnHack, ContentValues contentValues) {
SQLiteDatabase db = this.getWritableDatabase();
return db.insert(table, nullColumnHack, contentValues);
}
public Cursor rawQuery(String string, String[] selectionArguments) {
SQLiteDatabase db = this.getWritableDatabase();
return db.rawQuery(string, selectionArguments);
}
}
我已经检查了文件资源管理器,数据库已经复制并就位。但是,错误仍在发生。我从来没有遇到过其他实现相同数据库处理程序代码(Vocabdatabase)的应用程序的问题。我花了一天半的时间试图解决这个问题,但没有任何效果…您的问题是试图使用不包含数据库名称的路径打开 i、 e.无法打开14是为了
/data/user/0/com.example.hack.corrector/databases/
当打开时,应尝试打开
/data/user/0/com.example.hack.corrector/databases/ztr.db
不使用完整路径将导致两个问题,这很可能导致混淆
- 注释
//相同的代码在其他应用程序中工作。但是,我还是尝试了你的建议,但还是出现了同样的错误。有一件事:错误表明它无法打开数据库。@/data/user/0/com.example.hack.corrector/databases/。但当我在文件资源管理器中查找复制的数据库时,该数据库位于@/data/data/com.example.hack.corrector/databases/。但程序能够复制dbm,但无法在相同的地址is wierd中打开它。我最近将android studio更新为3.1。这可能是问题所在吗?如果是的话,我怎么解决呢?谢谢。那很有效!这似乎是新AS更新的问题..嗨!代码运行良好。但在重新安装并再次运行应用程序后,只创建了大小为12kb的空数据库@/data/data/com.example.corrector/databases。我把它拿出来检查。没有表,但名称相同:ztr.db。未复制任何表:(资产中的db文件为16Mb。。
public class VocabDatabase extends SQLiteOpenHelper { //The Android's default system path of your application database. //private static String DB_PATH = ""; //<<<< RMVD private static String DB_PATH_ALT; //<<<< ADDED private static String DB_NAME = "ztr.db"; private 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 VocabDatabase(Context context) { super(context, DB_NAME, null, 1); this.myContext = context; //this.DB_PATH = context.getApplicationInfo().dataDir + "/databases/"; //<<<< RMVD this.DB_PATH_ALT = context.getDatabasePath(DB_NAME).getPath(); //<<<< ADDED } /** * Creates a empty database on the system and rewrites it with your own database. */ public void createDataBase() throws IOException { //boolean dbExist = checkDataBase(); //<<<< RMVD boolean dbExist = checkDataBaseAlt(); //<<<< CHANGED 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"); } } } //<<<< ADDED Alternative method checks the file rather than database //<<<< as such no open error 14 messages /** * 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 checkDataBaseAlt() { //File chkdb = new File(myContext.getDatabasePath(DB_NAME).getPath()); //<<<< RMVD File chkdb = new File(DB_PATH_ALT); //<<<< ADDED return chkdb.exists(); } /** * 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() { this.getReadableDatabase(); SQLiteDatabase checkDB = null; try { //String myPath = DB_PATH; //<<<< RMVD so no open error 14 uses alt method checkDB = SQLiteDatabase.openDatabase( DB_PATH_ALT, //<<<< CHANGED null, SQLiteDatabase.OPEN_READWRITE ); } catch (SQLiteException e) { e.printStackTrace(); } if (checkDB != null) { checkDB.close(); } return checkDB != null; //<<<< simplified } /** * 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); //<<<< CHANGED // 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(DB_PATH_ALT); //<<<< CHANGED //transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length = 0; 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; //<<<< RMVD myDataBase = SQLiteDatabase.openDatabase( DB_PATH_ALT, //<<<< CHANGED 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) { if (newVersion > oldVersion) { try { copyDataBase(); } catch (Exception e) { e.printStackTrace(); } } } // Add your public helper methods to access and get content from the database. // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy // to you to create adapters for your views. //add your public methods for insert, get, delete and update data in database. public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) { SQLiteDatabase db = this.getWritableDatabase(); return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy); } public long insert(String table, String nullColumnHack, ContentValues contentValues) { SQLiteDatabase db = this.getWritableDatabase(); return db.insert(table, nullColumnHack, contentValues); } public Cursor rawQuery(String string, String[] selectionArguments) { SQLiteDatabase db = this.getWritableDatabase(); return db.rawQuery(string, selectionArguments); } }