gpt4 book ai didi

android - sqlite - 附加数据库

转载 作者:IT王子 更新时间:2023-10-29 06:25:40 26 4
gpt4 key购买 nike

我遵循了我在网上找到的一些关于附加 sqlite 数据库的信息,目的是将一个表从一个 sqlite 数据库复制到另一个数据库,但我似乎无法让它工作。我尝试使用此代码附加数据库:

DB_PATH = context.getDatabasePath("WineDB.sqlite").toString();
SQLiteDatabase backupDatabase = backupDBHandler.getWritableDatabase();
backupDatabase.execSQL("ATTACH '" + DB_PATH + "' AS 'tempDb'");

到目前为止它运行没有错误。然后我尝试通过从 tempDb 复制它来在备份数据库中创建一个新表:

sqlDB.execSQL("CREATE TABLE my_Producent AS SELECT * FROM tempDb.my_Producent");

它崩溃并出现错误“没有这样的表”tempDb.my_Producent“但是我确定该表存在于数据库”WineDB.sqlite“中。我在将数据库附加到 backupDatabase 之前调用的 onCreate 方法中创建它.

感谢您的帮助干杯user2302510

最佳答案

@菲尔。是的,我已经开始工作了。但是我真的不记得真正的问题是什么了,所以我只写一个简化的步骤序列,我在我的代码中找到了关于从 firstDB 复制到 secondDB。我在 SQLiteOpenHelper 中为 firstDB 执行以下操作。

public class firstDBHandler extends SQLiteOpenHelper {

SQLiteDatabase firstDB;
//FIRST_DB_NAME is something like 'xxx1.sqlite'
private static String FIRST_DB_PATH = context.getDatabasePath(FIRST_DB_NAME).toString();

public void copyFromFirstToSecond(String secondDBName, int secondDBVersion) {
//use the same context as for the firstDBHandler
secondDBHandler = new SecondDBHandler(myContext, secondDBName, secondDBVersion);
//here you create the secondDB if it does not exist yet
try {
secondDBHandler.createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
SQLiteDatabase secondDB = secondDBHandler.getWritableDatabase();

//note there are single quotation marks around FIRST_DB_PATH but none around tempDb
secondDB.execSQL("ATTACH DATABASE '" + FIRST_DB_PATH + "' AS tempDb");

//here you start to copy the tables from firstDB first by checking if the table exists in secondDB (secondDB is now the 'main' one, tempDB is the attached firstDB
secondDB.execSQL("DROP TABLE IF EXISTS main." + SECOND_DB_TABLE_NAME);

//here you create a table as a copy of
secondDB.execSQL("CREATE TABLE main." + SECOND_DB_TABLE_NAME + " AS SELECT * FROM tempDb." + FIRST_DB_TABLE_NAME);

//you can run the last two lines of code as many times as you need to copy all of the tables

//after you have copied all of them, you need to detach the tempDB
secondDB.execSQL("DETACH tempDb");
}

}

public class SecondDBHandler extends SQLiteOpenHelper {

//SECOND_DB_NAME is something like 'xxx2.sqlite'
private static String SECOND_DB_PATH = context.getDatabasePath(SECOND_DB_NAME).toString();
public void createDataBase() throws IOException {

boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
} else {
//This calls onCreate method of this SecondDBHandler, where you
//create tables that you need initially (but not those which you
//intent to copy from firstDB)
this.getReadableDatabase();
this.close();
}
}

public boolean checkDataBase() {
File file = new File(SECOND_DB_PATH);
return file.exists();
}
}

然后你只需调用:

FirstDBHandler firstDBHandler = new FirstDBHandler(getBaseContext(), FIRST_DB_NAME, FIRST_DB_VERSION);
firstDBHandler.getReadableDatabase();
firstDBHandler.copyFromFirstToSecond(SECOND_DB_NAME, SECOND_DB_VERSION);

当然 SQLiteOpenHandler 都缺少 onCreate 方法,但这取决于应用程序在那里做了什么。

希望答案没有错误。我自己没有测试过,只是从我更复杂的代码中提取了一个简化版本。如果它有一些赞成票,我会将其标记为答案。

关于android - sqlite - 附加数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20205406/

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