简体   繁体   中英

How to update data using room database?

I am using Room database and I want to update a row. I am able to update the row but getting this warning

12-05 20:59:17.635 29363-29372/com.example.parmarravi21.recyclerviewiv W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.example.parmarravi21.recyclerviewiv/databases/RecP10' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

This is my code

//defining database
        db = Room.databaseBuilder(getActivity(), AppDatabase.class, "RecP10")
                .fallbackToDestructiveMigration()
                .allowMainThreadQueries().build();

      TimeDateModel timeDateModel = new TimeDateModel(positionTime, TimeMode);

                        ContentValues contentValues = new ContentValues();
                        contentValues.put("posItem", timeDateModel.getPosItem());
                        contentValues.put("TimeDateMode", timeDateModel.getTimeDateMode());

                        if (db.timeDao().getItemAtTimePos(positionTime) != null) {
                            Log.e(TAG, "updated database");
                            db.beginTransaction();
                            try {
                                db.getOpenHelper().getWritableDatabase().update("Datetime", 0,
                                                contentValues, "posItem =" + positionTime, null);
                                db.setTransactionSuccessful();
                            } finally {
                                db.endTransaction(); // commit or rollback
                                db.close();

                            }
                        } else {
                            Log.e(TAG, "New item added");
                            db.timeDao().insertAll(timeDateModel);
                        }

I have also tried the dao update function but the data is not updating .

db.timeDao().update(timeDateModel);

I am new to database in android please help me out .

Github link for complete code

@Query("UPDATE Test SET test_name=:arg1,test_startDate=:arg2,test_endDate=:arg3,test_location=:arg4,test_ringerMode=:arg5,test_latitude=:arg6,test_longitude=:arg7 WHERE testId = :arg0")
fun updateTest( testId: Int,testName:String,testStartTime:String,testEndTime:String,testLocation:String,testRingerMode:Int,testLatitude:Double,testLongitude:Double)

Try coding this way.

Using an @Update annotation should do the trick , and according to the Android developer Documentation, you shouldn't perform database Queries on the main Thread ,thus might cause your database issues

Something like these

@Update
void updateTimeEntry(TimeEntry timeEntry)

with TimeEntry being the name of your model class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM