我有: 安卓6.0.1 sqlite 数据库助手
当我在开始和结束之间添加多个操作时,当我运行应用程序并创建数据库时,出现错误:
FATAL EXCEPTION: main
Process: com.infoman.liquideconomycs, PID: 26518
java.lang.RuntimeException: Unable to create application com.infoman.liquideconomycs.Core: android.database.sqlite.SQLiteException: near "(": syntax error (code 1): , while compiling: CREATE TRIGGER bulk_update_main
BEFORE INSERT ON main
WHEN EXISTS (SELECT pubKey FROM main WHERE pubKey = NEW.pubKey AND age + 0 < NEW.age + 0)
BEGIN
UPDATE main
SET (age) = (NEW.age)
WHERE pubKey = NEW.pubKey; INSERT INTO mainCount (age, count_) VALUES (NEW.age, 1);
SELECT raise(IGNORE);
END;
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4744)
at android.app.ActivityThread.access$1600(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1408)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.database.sqlite.SQLiteException: near "(": syntax error (code 1): , while compiling: CREATE TRIGGER bulk_update_main
BEFORE INSERT ON main
WHEN EXISTS (SELECT pubKey FROM main WHERE pubKey = NEW.pubKey AND age + 0 < NEW.age + 0)
BEGIN
UPDATE main
SET (age) = (NEW.age)
WHERE pubKey = NEW.pubKey; INSERT INTO mainCount (age, count_) VALUES (NEW.age, 1);
SELECT raise(IGNORE);
END;
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
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:1676)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1607)
at com.infoman.liquideconomycs.DBHelper.onCreate(DBHelper.java:38)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.infoman.liquideconomycs.Core.onCreate(Core.java:53)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1014)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4741)
... 8 more
创建表和触发器的源代码:
public void onCreate(SQLiteDatabase db) {
Log.d(LOG_TAG, "--- onCreate database ---");
//our pubKeys
db.execSQL("create table users ("
+ "id integer primary key autoincrement,"
+ "pubKey BLOB,"
+ "privKey BLOB" + ");");
db.execSQL("create table mainCount ("
+ "age integer primary key,"
+ "count_ Long);");
db.execSQL("create table main ("
+ "pubKey blob primary key,"
+ "age integer);");
db.execSQL("CREATE INDEX idx_date ON main (age);");
db.execSQL("CREATE TRIGGER bulk_insert_main\n" +
"BEFORE INSERT ON main\n" +
"WHEN NOT EXISTS (SELECT pubKey FROM main WHERE pubKey = NEW.pubKey)\n" +
"BEGIN\n" +
" INSERT INTO mainCount (age, count_) VALUES (NEW.age, 1);\n" +
"END;");
db.execSQL("CREATE TRIGGER bulk_update_main\n" +
"BEFORE INSERT ON main\n" +
"WHEN EXISTS (SELECT pubKey FROM main WHERE pubKey = NEW.pubKey AND age + 0 < NEW.age + 0)\n" +
"BEGIN\n" +
" UPDATE main SET (age) = (NEW.age)\n" +
" WHERE pubKey = NEW.pubKey;\n" +
" INSERT INTO mainCount (age, count_) VALUES (NEW.age, 1);\n" +
" SELECT raise(IGNORE);\n" +
"END;");
db.execSQL("CREATE TRIGGER bulk_new_update_main\n" +
"BEFORE UPDATE ON main\n" +
"BEGIN\n" +
" INSERT INTO mainCount (age, count_) VALUES (OLD.age, -1);\n" +
"END;");
db.execSQL("CREATE TRIGGER bulk_ignore_main\n" +
"BEFORE INSERT ON main\n" +
"WHEN EXISTS (SELECT pubKey FROM main WHERE pubKey = NEW.pubKey AND age + 0 >= NEW.age + 0)\n" +
"BEGIN\n" +
" SELECT raise(IGNORE);\n" +
"END;");
db.execSQL("CREATE TRIGGER bulk_update_mainCount\n" +
"BEFORE INSERT ON mainCount\n" +
"WHEN EXISTS (SELECT * FROM mainCount WHERE age = NEW.age)\n" +
"BEGIN\n" +
" UPDATE mainCount\n" +
" SET (count_) = (count_ + NEW.count_)\n" +
" WHERE age = NEW.age;\n" +
" SELECT raise(IGNORE);\n" +
"END;");
}
此问题仅出现在 Android 6 上,在更高版本上一切正常
注意:当在开始和结束之间添加第二个操作时,AndroidStudio 以红色突出显示“INSERT”:
db.execSQL("CREATE TRIGGER bulk_update_main\n" +
"BEFORE INSERT ON main\n" +
"WHEN EXISTS (SELECT pubKey FROM main WHERE pubKey = NEW.pubKey AND age + 0 < NEW.age + 0)\n" +
"BEGIN\n" +
" UPDATE main SET (age) = (NEW.age)\n" +
" WHERE pubKey = NEW.pubKey;\n" +
" INSERT INTO mainCount (age, count_) VALUES (NEW.age, 1);\n" +
" SELECT raise(IGNORE);\n" +
"END;");
并报告错误: 预计结束,得到“插入”
尝试删除触发器中的其他操作,在更高版本的 Android 中尝试过
问题已解决: 在创建触发器bulk_update_mainCount和创建触发器bulk_update_main中 我们需要删除 SET "SET Age" 和 "SET count_" 中的括号