没有插入数据并且在android sqlite中也没有抛出异常

时间:2018-08-11 11:45:33

标签: java android sqlite

我正在尝试在SQLite数据库中插入一些数据,但是没有插入数据,也没有引发任何异常。 我可能遗漏了一些明显的东西,但我无法弄清楚。 这是我插入数据的代码:

    public void addTransactionToDatabase(Transaction transactionClass) {

    String query = "INSERT INTO " + TABLE_TRANSACTION__NAME + " ( " + COLUMN_ACCOUNT_TYPE_NAME + "," + COLUMN_AMOUNT_NAME + ", " +
            COLUMN_CATEGORY_NAME + ", " + COLUMN_TIME_NAME + ", " + COLUMN_DATE_NAME + ", " + COLUMN_TIMESTAMP_NAME + ", " + COLUMN_REASON_NAME + " )" + " VALUES " + "( " +
            "\'" + transactionClass.getAccount() + "\', \'" + transactionClass.getAmount() + "\', \'" + transactionClass.getCategory() + "\', \'" +
            transactionClass.getTime() + "\', \'" + transactionClass.getDate() + "\', DATETIME(\'now\')" + ", " + "\'" + transactionClass.getReason() + "\'" + ");";
    SQLiteDatabase db = this.getWritableDatabase();
    Log.i(LOG_TAG, query);
    try {

        db.rawQuery(query, null);

    } catch (SQLException e) {
        Log.e(LOG_TAG, "Error while inserting transaction: " + e.getMessage());
    }
    db.close();
}

这是表的创建:

        db.execSQL("CREATE TABLE " + TABLE_TRANSACTION__NAME + " ( " + COLUMN_TIMESTAMP_NAME + " TEXT PRIMARY KEY, " +
            COLUMN_REASON_NAME + " TEXT, " + COLUMN_CATEGORY_NAME + " TEXT, " + COLUMN_AMOUNT_NAME + " INTEGER, " +
            COLUMN_ACCOUNT_TYPE_NAME + " TEXT, " + COLUMN_TIME_NAME + " TEXT, " + COLUMN_DATE_NAME + " TEXT, " +
            "FOREIGN KEY (" + COLUMN_ACCOUNT_TYPE_NAME + ") REFERENCES " + TABLE_ACCOUNT_NAME + "(" + COLUMN_ACCOUNT_TYPE_NAME + ") , " +
            "FOREIGN KEY ( " + COLUMN_CATEGORY_NAME + ") REFERENCES " + TABLE_CATEGORY_NAME + "(" + COLUMN_CATEGORY_NAME + ") )");

创建该表后,每次我检查它时,行计数都为零。

1 个答案:

答案 0 :(得分:3)

您必须致电db.execSQL(query);,而不是db.rawQuery(query, null);
rawQuery用于获取行。
您确定所有的\吗?