我正在尝试创建一个包含四个外键的表,其中两个指向同一个表。当我第一次启动活动时,我在logcat中通知了一个错误,但我找不到任何类型的错误。谢谢你的帮助
这是我的表格状态
public static final String TRANSACTION_TABLE_CREATE = "CREATE TABLE "
+ TRANSACTION_TABLE_NAME + " ("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_NAME + " TEXT NOT NULL, "
+ COLUMN_DESCRIPTION + " TEXT, "
+ COLUMN_TYPE + " INTEGER NOT NULL, "
+ COLUMN_AMOUNT + " REAL NOT NULL, "
+ COLUMN_STATUS + " INTEGER NOT NULL, "
+ COLUMN_ACCOUNT_ID + " INTEGER NOT NULL, "
+ COLUMN_CATEGORY_ID + " INTEGER NOT NULL, "
+ COLUMN_TRANSFER_ACCOUNT + " INTEGER NOT NULL, "
+ COLUMN_DATE + " TEXT NOT NULL, "
+ COLUMN_REGISTER_DATE + " TEXT NOT NULL, "
+ COLUMN_BUDGET_ID + " INTEGER, "
+ COLUMN_HOLDER + " TEXT, "
+ COLUMN_LATITUDE + " REAL, "
+ COLUMN_LONGITUDE + " REAL, "
+ "FOREIGN KEY (" + COLUMN_ACCOUNT_ID + ") REFERENCES " + AccountTable.ACCOUNT_TABLE_NAME + " (" + AccountTable.COLUMN_ID + "), "
+ "FOREIGN KEY (" + COLUMN_CATEGORY_ID + ") REFERENCES " + CategoryTable.CATEGORY_TABLE_NAME + " (" + CategoryTable.COLUMN_ID + "), "
+ "FOREIGN KEY (" + COLUMN_TRANSFER_ACCOUNT + ") REFERENCES " + AccountTable.ACCOUNT_TABLE_NAME + " (" + AccountTable.COLUMN_ID + "), "
+ "FOREIGN KEY (" + COLUMN_BUDGET_ID + ") REFERENCES " + BudgetTable.BUDGET_TABLE_NAME + " (" + BudgetTable.COLUMN_ID + ")"
+ ");";
这是我的logcat
>08-02 15:06:06.854 1110-1110/it.kage.crusoe E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{it.kage.crusoe/it.kage.crusoe.MainActivity}: android.database.sqlite.SQLiteException: near "transaction": syntax error (code 1): , while compiling: CREATE TABLE transaction (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, description TEXT, type INTEGER NOT NULL, amount REAL NOT NULL, status INTEGER NOT NULL, account_id INTEGER NOT NULL, category_id INTEGER NOT NULL, transfer_account INTEGER NOT NULL, date TEXT NOT NULL, register_date TEXT NOT NULL, budget_id INTEGER, holder TEXT, latitude REAL, longitude REAL, FOREIGN KEY (account_id) REFERENCES account (_id), FOREIGN KEY (category_id) REFERENCES category (_id), FOREIGN KEY (transfer_account) REFERENCES account (_id), FOREIGN KEY (budget_id) REFERENCES budget (_id));
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "transaction": syntax error (code 1): , while compiling: CREATE TABLE transaction (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, description TEXT, type INTEGER NOT NULL, amount REAL NOT NULL, status INTEGER NOT NULL, account_id INTEGER NOT NULL, category_id INTEGER NOT NULL, transfer_account INTEGER NOT NULL, date TEXT NOT NULL, register_date TEXT NOT NULL, budget_id INTEGER, holder TEXT, latitude REAL, longitude REAL, FOREIGN KEY (account_id) REFERENCES account (_id), FOREIGN KEY (category_id) REFERENCES category (_id), FOREIGN KEY (transfer_account) REFERENCES account (_id), FOREIGN KEY (budget_id) REFERENCES budget (_id));
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
at it.kage.crusoe.database.DatabaseHelper.onCreate(DatabaseHelper.java:42)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
at it.kage.crusoe.database.adapters.DatabaseAdapter.open(DatabaseAdapter.java:34)
at it.kage.crusoe.database.adapters.DatabaseAdapter.<init>(DatabaseAdapter.java:26)
at it.kage.crusoe.database.adapters.AccountAdapter.<init>(AccountAdapter.java:33)
at it.kage.crusoe.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
... 11 more
答案 0 :(得分:3)
“Transaction”是SQLite中的reserved word。您需要选择不同的名称或用双引号或括号引用标识符。建议您引用所有标识符(如果它们是英文单词),以便在将来将该单词作为保留关键字添加时,您的代码不会中断。