我正在创建一个数据库,该数据库引用另一个数据库中的键.本质上,我要导入的数据要与将要更改的数据分开,但是如果可能的话,我想通过外键引用其他键.据我所知,我需要先附加数据库才能实现这一点.到目前为止,这是相关的代码:
public class LogDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "log.db";
private static final int DATABASE_VERSION = 1;
private String namesDb;
public LogDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
namesDb=getNamesDbPath();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("ATTACH DATABASE ? AS names",new String[]{namesDb});
StringBuilder query=new StringBuilder();
query.append("CREATE TABLE log (");
}
}
但是,这似乎不起作用,因为出现了以下错误:
11-21 17:35:58.176: E/SQLiteLog(9984): (1) statement aborts at 5: [ATTACH DATABASE ? AS names] cannot ATTACH database within transaction
11-21 17:35:58.176: D/AndroidRuntime(9984): Shutting down VM
11-21 17:35:58.176: W/dalvikvm(9984): threadid=1: thread exiting with uncaught exception (group=0x41a21700)
11-21 17:35:58.191: E/AndroidRuntime(9984): FATAL EXCEPTION: main
11-21 17:35:58.191: E/AndroidRuntime(9984): android.database.sqlite.SQLiteException: cannot ATTACH database within transaction (code 1)
11-21 17:35:58.191: E/AndroidRuntime(9984): at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
11-21 17:35:58.191: E/AndroidRuntime(9984): at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:734)
11-21 17:35:58.191: E/AndroidRuntime(9984): at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
11-21 17:35:58.191: E/AndroidRuntime(9984): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
11-21 17:35:58.191: E/AndroidRuntime(9984): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
11-21 17:35:58.191: E/AndroidRuntime(9984): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
11-21 17:35:58.191: E/AndroidRuntime(9984): at com.kd7uiy.hamfinder.LogDatabaseHelper.onCreate(LogDatabaseHelper.java:27)
11-21 17:35:58.191: E/AndroidRuntime(9984): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
11-21 17:35:58.191: E/AndroidRuntime(9984): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
我该如何进行这项工作?