2016-11-29 59 views
0

我正在嘗試創建在其數據庫上使用觸發器的應用程序。每當我嘗試在Android Studio上運行它時,應用程序運行良好,代碼中沒有錯誤。但是當我嘗試在MEmu或Lollipop手機上運行它時,會顯示一些錯誤消息。我試圖修復它,但現在有一條消息,當我試圖在修復後啓動它時。SQLite觸發啓動android應用程序時出錯

了java.lang.RuntimeException:無法啓動活動ComponentInfo {com.dexpensemanager/com.dexpensemanager.Home}:android.database.sqlite.SQLiteException:限定表名不允許在INSERT,UPDATE和DELETE觸發器中的語句(代碼1):,編譯時:CREATE TRIGGER(如果不存在)sumcalc_income當TRANS.type =收入時,爲每行執行TRANS TRANSFER INSERT TRANSFORMAT =收入AND strftime('%Y' - '%m','now','start ')BEGIN插入SUMTRANS.income SELECT SUM(收入)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上$ android.os.Looper.loop(Looper.java:194) 在android.app.ActivityThread.main(ActivityThread.java:5546) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at 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 ):,編譯時:如果不存在CREATE TRIGGER sumcalc_income當TRANS.type = income AND strftime('%Y' - '%m','now','month of'')時,爲每行執行插入操作BEGIN insert到SUMTRANS.income SELECT SUM(收入)FROM TRANS.amount END; at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:898) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509 ) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) at 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 Reflective

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; 
} 

有人能告訴我該怎麼辦?

謝謝。

+0

在這個數據庫中有2個表Transaction_table和sumtrans –

回答

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修改表的名稱,刪除或插入 語句必須是一個不合格的表名。換句話說,當指定 表時,必須使用「表名」而不是「database.tablename」。要修改的表必須與觸發器所附的表或視圖在同一個數據庫中。

  • 不支持INSERT語句 的「INSERT INTO table DEFAULT VALUES」形式。

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

  • UPDATE和DELETE語句的ORDER BY和LIMIT子句不支持 。 ORDER BY和LIMIT通常不支持 UPDATE或DELETE在任何上下文中,但可以使用SQLITE_ENABLE_UPDATE_DELETE_LIMIT編譯時間 選項爲頂級 語句啓用。但是,該編譯時選項僅適用於頂級 UPDATE和DELETE語句,而不適用於 觸發器中的UPDATE和DELETE語句。

  • 公用表表達式不支持 觸發器內的語句。

我想SUMTRANS在聲明insert into SUMTRANS.expense是數據庫名稱。根據文檔,這不被支持。

希望這會有所幫助。

+0

哇,我認爲它的支持。我想不是所有的參考都是有效的,那麼謝謝 –

相關問題