SQLite触发启动Android应用程序时出错

时间:2016-11-29 23:03:18

标签: android sql database sqlite triggers

我正在尝试创建一个在其数据库上使用触发器的应用。每当我尝试在Android Studio上运行它时,应用程序运行良好,代码中没有错误。但是当我尝试在MEmu或Lollipop手机上运行时,它会向我显示一些错误消息。我已经尝试修复它,但是当我尝试在修复后启动它时,会出现此消息。

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.dexpensemanager / com.dexpensemanager.Home}:android.database.sqlite.SQLiteException:触发器中的INSERT,UPDATE和DELETE语句不允许使用限定的表名(代码1):,编译时:如果不存在,则创建TRIGGER。当TRANS.type =收入和strftime('%Y' - '%m' ,'现在','月初')BEGIN插入到SUMTRANS.income SELECT SUM(income)FROM TRANS.amount END;       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460)       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)       在android.app.ActivityThread.access $ 800(ActivityThread.java:169)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1421)       在android.os.Handler.dispatchMessage(Handler.java:111)       在android.os.Looper.loop(Looper.java:194)       在android.app.ActivityThread.main(ActivityThread.java:5546)       at java.lang.reflect.Method.invoke(Native Method)       在java.lang.reflect.Method.invoke(Method.java:372)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:967)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)   引起:android.database.sqlite.SQLiteException:触发器中的INSERT,UPDATE和DELETE语句不允许使用限定的表名(代码1):,编译时:如果不存在,则创建TRIGGER sumcalc_income在每次行转换后插入TRANS.type =收入和strftime('%Y' - '%m','现在','月初')BEGIN insert进入SUMTRANS.income SELECT SUM(income)FROM TRANS.amount END;       在android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法)       在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:898)       在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509)       在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)       在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58)       在android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31)       在android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1704)       在android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1635)       在com.example.connectionDB.DBnTrigger.onCreate(DBnTrigger.java:45)       在android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)       在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)       在com.example.connectionDB.DBnTrigger.getLastIncome(DBnTrigger.java:459)       在com.dexpensemanager.Home.onCreate(Home.java:61)       在android.app.Activity.performCreate(Activity.java:5975)       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)       ......还有10个

以及我的触发器的代码,我从create trigger参考

获取它
public String insincTrigger() {
    String insinc = "CREATE TRIGGER if not exists sumcalc_income "
            + " AFTER INSERT "
            + " ON "+TRANSACTION_TABLE
            + " FOR  EACH  ROW "
            + " WHEN "
            + " TRANS.type= income "
            + " AND  strftime ('%Y'-'%m','now','start of month') "
            + " BEGIN "
            + " insert into SUMTRANS.income "
            + " SELECT SUM(income) FROM TRANS.amount "
            + " END; ";

    return insinc;
}

public String insexpTrigger(){
    String insexp = "CREATE TRIGGER if not exists sumcalc_expense "
            + " AFTER INSERT "
            + " ON "+TRANSACTION_TABLE
            + " FOR  EACH  ROW "
            + " WHEN "
            + " TRANS.type= expense "
            + " WHERE  strftime ('%Y'-'%m','now','start of month') "
            + " BEGIN "
            + " insert into SUMTRANS.expense "
            + " SELECT SUM(expense) FROM TRANS.amount "
            + " END; ";
    return insexp;
}

public String upincTrigger(){
    String upinc = "CREATE TRIGGER if not exists usumcalc_income "
            + " AFTER UPDATE "
            + " OF [amount] "
            + " ON "+TRANSACTION_TABLE
            + " FOR  EACH  ROW "
            + " WHEN "
            + " TRANS.type= income "
            + " WHERE  strftime ('%Y'-'%m','now','start of month') "
            + " BEGIN "
            + " UPDATE OR REPLACE "+TRANSACTION_SUM
            + " SET [income] = SELECT SUM(income) from TRANS.amount "
            + " END; ";
    return upinc;
}

public String upexpTrigger() {
    String upexp = "CREATE TRIGGER if not exists usumcalc_expense "
            + " AFTER UPDATE "
            + " OF [amount] "
            + " ON "+TRANSACTION_TABLE
            + " FOR  EACH  ROW "
            + " WHEN "
            + " TRANS.type= expense "
            + " WHERE  strftime ('%Y'-'%m','now','start of month') "
            + " BEGIN "
            + " UPDATE OR REPLACE "+TRANSACTION_SUM
            + " SET [expense] = SELECT SUM(expense) from TRANS.amount "
            + " END; ";
    return upexp;
}

public String delTrigger(){
    String del = "CREATE TRIGGER if not exists sumcalc_delete "
            + " AFTER DELETE "
            + " ON "+TRANSACTION_TABLE
            + " FOR  EACH  ROW "
            + " WHEN "
            + " TRANS.type = income AND expense "
            + " WHERE  strftime ('%Y'-'%m','now','start of month') "
            + " BEGIN "
            + " DELETE FROM "+TRANSACTION_SUM
            + " WHERE = SELECT SUM(income AND expense) from TRANS.amount "
            + " END; ";
    return del;
}

public String instimeTrigger(){
    String instime = "CREATE TRIGGER if not exists sumcalc_instime "
            + " AFTER INSERT "
            + " ON "+TRANSACTION_SUM
            + " FOR  EACH  ROW "
            + " BEGIN "
            + " insert into SUMTRANS.month "
            + " VALUES ('%Y'-'%m','now','start of month') "
            + " END;";
    return instime;
}

public String deltimeTrigger(){
    String deltime = "CREATE TRIGGER if not exists sumcalc_instime "
            + " AFTER DELETE "
            + " ON "+TRANSACTION_SUM
            + " FOR  EACH  ROW "
            + " BEGIN "
            + " DELETE FROM SUMTRANS.month "
            + " END; ";
    return deltime;
}
有人可以告诉我该怎么办?

感谢。

1 个答案:

答案 0 :(得分:0)

异常堆栈显然是在说明问题:

android.database.sqlite.SQLiteException: qualified table names are not allowed on
INSERT, UPDATE, and DELETE statements within triggers (code 1)

来自问题中的链接SQLite docs(强调我的):

  

触发器中的UPDATE,DELETE和INSERT语句的语法限制

     

触发器中的UPDATE,DELETE和INSERT语句不支持UPDATE,DELETE和INSERT语句的完整语法。以下限制适用:

     
      
  • 要在UPDATE,DELETE或INSERT中修改的表的名称   语句必须是不合格的表名。换句话说, 必须   只使用" tablename"不是" database.tablename"在指定时   表。要修改的表必须存在于同一数据库中   触发器附加到的表或视图。

  •   
  • " INSERT INTO表DEFAULT VALUES" INSERT语句的形式   不受支持。

  •   
  • UPDATE不支持INDEXED BY和NOT INDEXED子句   和DELETE语句。

  •   
  • UPDATE和DELETE语句的ORDER BY和LIMIT子句是   不支持。通常不支持ORDER BY和LIMIT   在任何上下文中更新或删除,但可以为顶级启用   使用SQLITE_ENABLE_UPDATE_DELETE_LIMIT编译时的语句   选项。但是,该编译时选项仅适用于顶级   UPDATE和DELETE语句,而不是UPDATE和DELETE语句   触发器。

  •   
  • 内部语句不支持公用表表达式   触发器。

  •   

我认为语句SUMTRANS中的insert into SUMTRANS.expense是数据库名称。根据文档,不支持。

希望这有帮助。