gpt4 book ai didi

java - 没有这样的表: quotes (code 1 SQLITE_ERROR) : Android P

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:00 25 4
gpt4 key购买 nike

我有一个报价应用程序,其中使用 SqlLite 数据库来存储报价。当用户打开应用程序时,它将数据从 Assets 复制到数据库。它在所有版本中都工作正常,但在 Android P 中,它在应用程序启动时崩溃。

它给了我这样的错误

android.database.sqlite.SQLiteException: no such table: quotes (code 1 SQLITE_ERROR): , while compiling: SELECT COUNT(qu_text) AS count FROM quotes

我确信数据库不会被复制,因此不会出现表错误,但它仅适用于 Android P。其他版本工作正常。

我的DBHandler.java如下所示

public class DBHandler extends SQLiteOpenHelper {
private static String DB_PATH;
private static String DB_NAME = "xxx";
private SQLiteDatabase myDataBase;
private final Context myContext;

DBHandler(Context context) {

super(context, DB_NAME, null, constant.DATABASE_VERSION);
this.myContext = context;
DB_PATH = context.getDatabasePath(DB_NAME).toString();
}
void createDataBase() throws IOException {

boolean dbExist = checkDataBase();

if (dbExist) {
Log.e("database","exist");
} else {

this.getReadableDatabase();

try {
copyDataBase();

} catch (IOException e) {

throw new Error("Error copying database");

}
}

}

private boolean checkDataBase() {

SQLiteDatabase checkDB = null;

try {
String myPath = DB_PATH;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

} catch (SQLiteException e) {

// database does't exist yet.

}

if (checkDB != null) {

checkDB.close();

}

return checkDB != null;
}


private void copyDataBase() throws IOException {

InputStream myInput = myContext.getAssets().open(DB_NAME);

String outFileName = DB_PATH;

OutputStream myOutput = new FileOutputStream(outFileName);

byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}

myOutput.flush();
myOutput.close();
myInput.close();

}

// ==============================================================================

void openDataBase() throws SQLException {

String myPath = DB_PATH;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}


// ==============================================================================

@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) {

}

}

DAO.java 类具有如下功能

private void open() throws SQLException {
if(database!=null&&database.isOpen())return;
database = dbHandler.getWritableDatabase();

}


private void closeDAO(){
synchronized (lockObj){
if(dbHandler!=null)dbHandler.close();
dbHandler=null;
database=null;
}
}



public static void dispose(){

if(dBObject!=null){
dBObject.closeDAO();
}
dBObject=null;
}

正如一位用户在回答中所说,我在复制数据库之前尝试像下面这样关闭数据库连接,但仍然出现相同的错误。

private void copyDataBase() throws IOException {
SQLiteDatabase db = getReadableDatabase();
db.close();

InputStream myInput = myContext.getAssets().open(DB_NAME);

String outFileName = DB_PATH;

OutputStream myOutput = new FileOutputStream(outFileName);

byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}

myOutput.flush();
myOutput.close();
myInput.close();

}

如果有人可以帮助解决这个难题,请告诉我。谢谢

最佳答案

这是 Android P 中的预期行为。您可以在此处阅读详细信息 https://issuetracker.google.com/issues/80268903

要解决此问题,应用程序必须确保在复制文件之前没有与数据库的打开连接。

关于java - 没有这样的表: quotes (code 1 SQLITE_ERROR) : Android P,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52567582/

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