开发者

SQLiteDatabase.insertOrThrow doesn't throw but not data is inserted

开发者 https://www.devze.com 2023-01-25 11:58 出处:网络
I\'m trying to get familiar with Android and its database API. I\'ve created a class that inherits from SQLiteOpenHelper and this

I'm trying to get familiar with Android and its database API. I've created a class that inherits from SQLiteOpenHelper and this is how I create the table in the database

@Override
public void onCreate(SQLiteDatabase db) {

    try {
        db.execSQL("CREATE TABLE " + FUELS_TABLE_NAME + " ("
                + "_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "DATE_OF_FUELS DATE DEFAULT CURRENT_TIME,"
                + "SELLER_POSITION TEXT DEFAULT 'unknown',"
                + "AMOUNT REAL"
                + ");"
        );
    } catch (SQLException e) {
        Log.e(DATABASE_NAME, e.toString());
    }
}

The function used to add data to the DB is the following implemeneted within the same class is

public void addNewFuel(float amount) {

    // Create the content to insert into the database
    ContentValues newEntry = new ContentValues();
    newEntry.put("amount", amount);

    // Get database handler
    try {
        db = getWritableDatabase();
    } catch (SQLException e) {
        Log.e(DATABASE_NAME, e.toString());
        return;
    }

    // Begin transaction and insert data
    long returnedValue;
    db.beginTransaction();
    try {
        returnedValue = db.insertOrThrow(FUELS_TABLE_NAME, null, newEntry);
        Log.v(DATABASE_NAME, "return value " + returnedValue);
    } catch (SQLException e) {
        Log开发者_运维知识库.e(DATABASE_NAME, e.toString());
    } finally {
        db.endTransaction();
    }

    db.close();
}

but apparently no data is added. The returnValue is always 1. The method doesn't throw, and when I pull out the DB with adb and look at it's content is totally empty.

I just can't understand what I'm missing.

Any suggestion would be appreciated.

Thanks, S


McStretch's answer is incorrect. getWritableDatabase() does not create a transaction for your code, the quoted line from the docs is referring to transactions being used for the onCreate and onUpgrade methods meaning that you don't need to add transaction code in those methods. You still need add transaction code for any other method that requires transactions.

emitrax's code is not working correctly as db.setTransactionSuccessful() is not being called which means the transaction will be rollbacked by db.endTransaction().


See benritz's answer for the correct solution. This answer is incorrect, but I'm unfortunately not able to delete it since it's an accepted post.

/******* NOT CORRECT!!

Since you're inheriting from SQLiteOpenHelper, your call to getWritableDatabase() already starts a DB transaction. From the SQLiteOpenHelper API:

Transactions are used to make sure the database is always in a sensible state.

Thus you don't need to call db.beginTransaction() and db.endTransaction(). Those extraneous transaction calls are messing up your inserts. I plugged the same scenario into my project and found that the same index (6 in my case), was returned when using those transaction methods. When I remove the calls I get my desired results (multiple records inserted).

NOT CORRECT!! *******/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号