I want to insert list of objects into my SQLite db. If I do query for each object to check if its present, it takes alot of time for long list (20-30sec).
I used function 'insertWithOnConflict()' to do that faster.
I thought that it will try to insert a row and if record with same value of column entered to function already exists, it will just replace its values (like update) otherwise it will insert new row.
But I checked my DB and it is creating duplicated objects. Any solution for this?
Important information 'COLUMN_OBJECT_ID' is not 'PRIMARY_KEY'. 'PRIMARY_KEY' is autoincrement. But 'COLUMN_OBJECT_ID' has to be unique in this case. So I want to update row with same 'COLUMN_OBJECT_ID' because data inside can change.
Code:
fun saveObjectsToCache(objects: List<Item>) {
val db = getDb()
db.transaction {
objects.forEach {item->
val cv = ContentValues()
cv.apply {
put(COLUMN_OBJECT_ID, item.id)
put(COLUMN_OBJECT_DATA, item.js.toString())
}
insertWithOnConflict(TABLE_OBJECT_CACHE, COLUMN_OBJECT_ID, cv, SQLiteDatabase.CONFLICT_REPLACE)
}
}
}
Looks like you forgot to add a unique constraint on COLUMN_OBJECT_ID.
Because of this, there is no conflict when you add a row with the same COLUMN_OBJECT_ID.
Make COLUMN_OBJECT_ID unique and it should work.
Additionally, if COLUMN_OBJECT_ID is unique, and is what you use for checking existing values in the table, you should probably make it the primary key, instead of the auto increment one you are using right now as the primary key.
It will be more efficient both in memory usage, and in performance
Related
I am working on Room database and trying to insert list of items(eg. list of Quotes which contains author name and a quote in my case).
Following is the code I am using:
// view model
BaseApp.daoInstance?.appDao()?.insertQuotes(response!!)
// dao
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertQuotes(listData: MutableList<Quote>)
When I try to insert the same data again, it always inserts as a new data instead of replacing with the current items.
I have researched a lot for this OnConflictStrategy.REPLACE but could not find any proper answer.
Is there anyone facing the same issue and found solution or am I doing anything wrong?
Thank you in advance...!!!
Room, will not check and compare if you have the quote already in the DB.
What it will do is look if the primary key already exists in the DB if it does, Room will replace all old data with the new one.
In your case, you are not specifying an ID so the DB is generating a unique one for you.
What you should do is create a Query that will search for this quote in the DB something like this:
#Query("SELECT * from quote_table WHERE author = :author AND quote = :quote")
List<Quote> getQuoteByAuthorAndQuote(string author, string quote);
This should return a list with a single quote if one is found and empty if it does not exist.
If you would like to override the old one just update the data in the Quote POJO and insert it to the DB using Room.
Have you tried to index your main column and mark it as unique?
#Index(value = {"quote"}, unique = true)}
It suppose to search for your unique or primary key and compare then replace, while in your case you're not defining an ID so it will generate a unique one for you, so it won't even compare and will consider any item as a new one.
Write a new query and function to solve this issue.
When I had same problem, changes in imports did the trick, added following import:
import androidx.room.*;
I have the below method which inserts values in my sqlite db. I'm trying to update the code to handle situations where "carid" and "sellerno" already exist in the table and if they do to replace with the new values being inserted. Any help is appreciated.
public void addListItem(String carid,String sellerno,String condition,String dat) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(carid, carid);
values.put(sellerno, sellerno);
values.put(cond, condition);
values.put(updatetime, dat);
db.insert(TABLE_CARS, null, values);
db.close();
}
There are two requirements:
SQLite needs to know that the combination of carid and sellerno must be unique for all rows in the table.
SQLite needs to know what to do when an insert or update results in a conflict (more than one row with the same combination of values for those columns).
You can do both of these at once by modifying your CREATE TABLE command as follows:
CREATE TABLE tableName (
column1 ...,
column2 ...,
...,
UNIQUE(column1, column2) ON CONFLICT REPLACE)
Now any insert/update will automatically replace the values in existing rows when the insert/update would create a conflict.
However, there may be situations where you do NOT want to replace the values in the row when there is a conflict. In those cases, you should specify the conflict algorithm in the insert/update itself (using this or this), which will override the replace algorithm specified above. Alternatively, you can leave off the ON CONFLICT REPLACE above and just use regular inserts/updates, but then you must insert/update with conflict when you want to replace.
You can read more about conflict algorithms here.
The update code is very similar to what you have now.
You have to add the new values but the same ids that you want, and then instead of the method insert(...) you use replace(...) or update(...).
db.update(TABLE_CARS, values, "_id "+"="+1, null);
// The third argument above is the where clause.
The other way would be:
db.replace(TABLE_CARS, null, values);
The rest of your code is the same. Just change the insert line.
I'm using Ormlite for database operations on my Android App.
In one of the methods, I need to delete the last row in a table. However I m not able to find out how. I'm successfully deleting rows when giving an argument, but in this specific case I don't have arguments.
Here's my attempt:
LottoDatabaseHelper helper = OpenHelperManager.getHelper(getApplicationContext(), DatabaseHelper.class);
//You get helper
Dao dao = helper.getDao(MyTable.class);
//get your Dao
DeleteBuilder<MyTable, Integer> deleteBuilder = dao.deleteBuilder();
//How can I specify last row here?
deleteBuilder.delete();
Thank you for your help
I haven't used Ormlite .. here is a simple solution which can use with any database ... As you said you can delete a row by passing argument...
try this..
1. Find the last id using the query..
eg. SELECT id FROM table_name ORDER BY id DESC LIMIT 1
// will return the last row id
2. then pass the id as a parameter to delete ...
I have a case that I would like to insert record in SQLite with database.insert(table, null, values).
TABLE t2 (_id, field1, field2)
..
val.setVal1(null);
val.setVal2(val2);
..
if(val.getVal1==null){
values.put(field1, _id);
}else{
values.put(field1, var.val1);
}
values.put(field2, var.val2);
database.insert("t2", null, values);
Is possible to do sth like this "values.put(field1, _id);"?
_id is generated at database.insert().
Note: I am looking for solution for one insert call. Insert and update row with (field1=_id) is easy.
i think i see now. you're asking if you can enter a value into a specific SQLite row _id field if it's available in your val object. Else, you want the database to automatically create a unique id for that column while inserting, like normally done. Is this correct?
To that end, i would seriously reconsider this purpose. You should never be specifying values for the _id column because it needs to be unique or else you'll get exceptions thrown. Moreover, it's only purpose is to be a unique identifier for the system, so you personally knowing this value should be of no use to you.
If you still need this functionality, i'd suggest making another field in your table (much like the _id column but not it), which you can fill with randomly generated numbers or val.getVal1 values.
PROBLEM BACKGROUND:
I have two tables where the Primary Key's are Text fields.
When I repeatedly use SQLiteDatabase.insertWithOnConflict(myTable, null, myValues, SQLiteDatabase.CONFLICT_REPLACE) with the same data, I see ever increasing values being returned.
So it looks like INSERT is being performed (instead of REPLACE) for the same data sets.
PROBLEM:
However, looking at these tables with "SQLite Database Browser" I see the correct number of records that I'd expect when REPLACE is performed.
The root to my confusion is the description Android documentation that states :
*Returns
the row ID of the newly inserted row OR the primary key of the existing row if the input param 'conflictAlgorithm' = CONFLICT_IGNORE OR -1 if any error*
QUESTION:
Why is it that a REPLACE is done, but the row ID of the existing data isn't returned ?
Seems to me like it is doing exactly what you are asking it to? You are asking it to replace and it does exactly that. It will remove the old row and insert the new one.
It would leave the old row and return that ID if you instead passed CONFLICT_IGNORE.
try{
insertWithOnConflict(openHelper_ob.TABLE_NAME,null,contentValues,
SQLiteDatabase.CONFLICT_ABORT);
}
catch(SQLException ex)
{
Toast.makeText(getApplicationContext(),"name found before plz, change it", Toast.LENGTH_SHORT).show();
}