Android SQLite多表输入

时间:2012-11-13 16:21:03

标签: android sqlite multiple-tables

我有一个预算程序,它使用多个表来存储信息。当我尝试一次向多个表添加信息时,我最终只得到第一个表接收任何信息。这是我使用的代码:

public SQLiteDatabase createBudget(String bname, String bpay, String bamount,
                String bmemo) {
            // TODO Auto-generated method stub
            ContentValues t1 = new ContentValues();
            ContentValues t2 = new ContentValues();
            ContentValues t4 = new ContentValues();
            t1.put(KEY_NAME, bname);
            t1.put(KEY_PAYDAY,bpay);
            t2.put(KEY_AMOUNT, bamount);
            t4.put(KEY_MEMO, bmemo);
            ourDatabase.insert(DATABASE_TABLE1, null, t1);
            ourDatabase.insert(DATABASE_TABLE2, null, t2);
            ourDatabase.insert(DATABASE_TABLE4, null, t4);
            return ourDatabase;
        }

我完全迷失了该怎么做。我试过在网上看,但我找到的东西是单表数据库。我将不胜感激任何帮助。这是LogCat内容:

11-13 10:14:33.935: E/SQLiteDatabase(662): Error inserting budget_amount=99
11-13 10:14:33.935: E/SQLiteDatabase(662): android.database.sqlite.SQLiteConstraintException: Budget_Items.edit_date may not be NULL (code 19)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at budget.app.BudgetDB.createBudget(BudgetDB.java:161)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at budget.app.AddBudget$1.onClick(AddBudget.java:40)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at android.view.View.performClick(View.java:4084)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at android.view.View$PerformClick.run(View.java:16966)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at android.os.Handler.handleCallback(Handler.java:615)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at android.os.Looper.loop(Looper.java:137)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at java.lang.reflect.Method.invokeNative(Native Method)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at java.lang.reflect.Method.invoke(Method.java:511)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 10:14:33.935: E/SQLiteDatabase(662):  at dalvik.system.NativeStart.main(Native Method)
11-13 10:14:33.955: E/SQLiteDatabase(662): Error inserting _memo=for pie
11-13 10:14:33.955: E/SQLiteDatabase(662): android.database.sqlite.SQLiteConstraintException: Budget_Memos.edit_date may not be NULL (code 19)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at budget.app.BudgetDB.createBudget(BudgetDB.java:162)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at budget.app.AddBudget$1.onClick(AddBudget.java:40)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at android.view.View.performClick(View.java:4084)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at android.view.View$PerformClick.run(View.java:16966)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at android.os.Handler.handleCallback(Handler.java:615)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at android.os.Looper.loop(Looper.java:137)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at java.lang.reflect.Method.invokeNative(Native Method)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at java.lang.reflect.Method.invoke(Method.java:511)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 10:14:33.955: E/SQLiteDatabase(662):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:4)

我认为问题是“Budget_Items.edit_date可能不是NULL”。您的数据库中有一个NOT NULL列,并尝试插入行而不在列Budget_Items.edit_date

列中给出值

答案 1 :(得分:0)

您的约束异常在这里非常明显

android.database.sqlite.SQLiteConstraintException:Budget_Items.edit_date可能不是NULL(代码19)

尝试在调用周围放置一个try catch块以找到确切的错误。