android – sqlite – 附加数据库

前端之家收集整理的这篇文章主要介绍了android – sqlite – 附加数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我跟踪了我在网上发现的一些关于附加sqlite数据库的信息,目的是将一个表从一个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“中.我在onCreate方法中创建它,在将数据库附加到backupDatabase之前调用方法.

谢谢你的帮助
干杯
user2302510

解决方法

@Phil.是的,我有它的工作.但是我真的不记得真正的问题了,所以我只是编写一个简单的步骤,我在代码中找到了关于从firstDB复制到secondDB的步骤.我在firstite的sqliteOpenHelper中执行以下操作.
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方法,但这取决于应用程序在那里所做的事情.

希望答案不包含任何错误.我自己没有测试过,只是从我更复杂的代码提取了一个简化版本.如果它有几个upvotes我会将其标记为答案.

原文链接:https://www.f2er.com/android/309454.html

猜你在找的Android相关文章