我正在创建一个数据库,该数据库引用另一个数据库中的键.本质上,我要导入的数据要与将要更改的数据分开,但是如果可能的话,我想通过外键引用其他键.据我所知,我需要先附加数据库才能实现这一点.到目前为止,这是相关的代码:
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 transaction11-21 17:35:58.176: D/AndroIDRuntime(9984): Shutting down VM11-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: main11-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)
我该如何进行这项工作?
解决方法:
我想出了一种使它起作用的方法,虽然我对此并不感到特别骄傲,但是它确实起作用.如果附加数据库时无法进行事务处理,请先结束事务处理.
db.setTransactionSuccessful();db.endTransaction();db.execsql("ATTACH DATABASE ? AS names",new String[]{namesDb});db.beginTransaction();
总结 以上是内存溢出为你收集整理的使用SQLiteOpenHelper在Android中连接sqlite数据库全部内容,希望文章能够帮你解决使用SQLiteOpenHelper在Android中连接sqlite数据库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)