About SQLite 3, is it required to read after write? - android

In an Android application, concerning SQLITE3,
Is it required to read after write or we can just count on a successful transaction ?
Update to show the function to insert data:
// Insert a post into the database
public void addM0(Store_M0_DataClass mStore_M0_DataClass)
{
// Create and/or open the database for writing
SQLiteDatabase db = getWritableDatabase();
// It's a good idea to wrap our insert in a transaction. This helps with performance and ensures
// consistency of the database.
db.beginTransaction();
try
{
// The user might already exist in the database (i.e. the same user created multiple posts).
ContentValues values = new ContentValues();
values.put(KEY_M0_ID, mStore_M0_DataClass.getM0_ID());
values.put(KEY_M0_IMAGE1, mStore_M0_DataClass.getM0_Image1());
values.put(KEY_M0_IMAGE2, mStore_M0_DataClass.getM0_Image2());
values.put(KEY_M0_ENABLE, mStore_M0_DataClass.getM0_Enable());
db.insertOrThrow(TABLE_M0, null, values);
db.setTransactionSuccessful();
}
catch (Exception e)
{
}
finally
{
db.endTransaction();
}
}

An insert or any single action is automatically enclosed in a transaction, as such there is no benefit in wrapping it in a transaction. However, if you were doing multiple inserts in a loop then beginning a transaction prior to the loop, setting it as successful, if successful, after the loop, and ending the transaction after the loop it would have a benefit.
There is also little need to use insertOrThrow unless you want to trap an exception, as the insert method is effectively INSERT OR IGNORE and the result is the rowid of the inserted row, which will be 1 or greater (in most normal use cases).
rowid is a special, normally hidden column, frequently aliased using by using a column defined using specifically column_name INTEGER PRIMARY KEY or column_name INTEGER PRIMARY KEY AUTOINCREMENT Rowid Tables
(noting that AUTOINCREMENT is generally not required and has overheads) SQLite Autoincrement
As such using :-
public long addM0(Store_M0_DataClass mStore_M0_DataClass)
{
// Create and/or open the database for writing
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_M0_ID, mStore_M0_DataClass.getM0_ID());
values.put(KEY_M0_IMAGE1, mStore_M0_DataClass.getM0_Image1());
values.put(KEY_M0_IMAGE2, mStore_M0_DataClass.getM0_Image2());
values.put(KEY_M0_ENABLE, mStore_M0_DataClass.getM0_Enable());
return db.insert(TABLE_M0, null, values);
}
However, if you wanted to store multiple Store_M0_DataClass objects then you could have :-
public int addManyMo(Store_M0_DataClass[] mStore_M0_DataClasses) {
int rv = 0;
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();// <<<<<<<<<< wrap all inserts in a transaction
for (Store_M0_DataClass m0: mStore_M0_DataClasses) {
if(addM0(m0) > 0) {
rv++;
}
}
// Assumes OK if any inserts have worked, not if none have worked
if (rv > 0) {
db.setTransactionSuccessful();
}
db.endTransaction();
return rv; // Retruns the number of rows inserted
}
As for :-
Is it required to read after write or we can just count on a
successful transaction ?
A transaction being successful commits what was in the transaction. Comitting is effectively writing the data to disk so there is no need to do anything after a commit.
with WAL (Write Ahead logging) the data is written to the WAL file rather than the database (and also the shm file). However when accessing the database if the WAL has data it is effectively read as being part of the database. The WAL data is written to the database when the WAL is checkpionted.

Related

What is the difference between inserting data using ContentValues and Raw SQL in SQLite Android?

I want to know the difference between inserting data using ContentValues and inserting data using Raw SQL in SQLlite(Android), Is there a advantage using content values?
To perform insert, read, delete, update operation there are two different ways:
Write parameterized queries (Recommended)
Write raw queries
Parameterized Queries: These are those queries which are performed using inbuilt functions to insert, read, delete or update data. These operation related functions are provided in SQLiteDatabase class.
Raw Queries: These are simple sql queries similar to other databases like MySql, Sql Server etc, In this case user will have to write query as text and passed the query string in rawQuery(String sql,String [] selectionArgs) or execSQL(String sql,Object [] bindArgs) method to perform operations.
Important Note: Android documentation don’t recommend to use raw queries to perform insert, read, update, delete operations, always use SQLiteDatabase class’s insert, query, update, delete functions.
Following is an example of raw query to insert data:
public void insertItem(Item item) {
String query = "INSERT INTO " + ItemTable.NAME + " VALUES (0,?,?)";
SQLiteDatabase db = getWritableDatabase();
db.execSQL(query, new String[]{item.name, item.description});
db.close();
}
While using raw queries we never come to know the result of operation, however with parameterized queries function a value is returned for success or failure of operation.
Insert: To perform insert operation using parameterized query we have to call insert function available in SQLiteDatabase class. insert() function has three parameters like public long insert(String tableName,String nullColumnHack,ContentValues values) where tableName is name of table in which data to be inserted.
Here is simple example:
//Item is a class representing any item with id, name and description.
public void addItem(Item item) {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name",item.name);
// name - column
contentValues.put("description",item.description);
// description is column in items table, item.description has value for description
db.insert("Items", null, contentValues);//Items is table name
db.close();
}
For more Information see this Link

Quickest way to update SQLite database on android (or add record if it does not yet exist)

I have written the following code to update some information in a database using SQLite. The idea is to update a value (userAverageTime) in case the entry already exist, or make a new record if the entry does not yet exist.
Here is the code:
public void updateTableLevelsAverageTime(DatabaseOperations dop, LinkedList<Level> levels) {
SQLiteDatabase SQ = dop.getWritableDatabase();
String selection = KEY_NUMBERS + " = ?";
for (Level level: levels) {
String[] args = {level.toString()};
ContentValues cv = new ContentValues();
cv.put(KEY_AVERAGE_TIME, level.getAverageTime());
int result = SQ.update(TABLE_LEVELS, cv, selection, args);
if (result == 0) {
//If the entry doesn't exist, it must be a new level... thus add it with 0 as userseconds
ContentValues cv2 = new ContentValues();
cv2.put(KEY_NUMBERS, level.toString());
cv2.put(KEY_AVERAGE_TIME, level.getAverageTime());
cv2.put(KEY_USER_TIME, 0);
SQ.insert(TABLE_LEVELS, null, cv2);
Log.d("Database operations", "Row added");
}
else {
Log.d("Database operations", "Row updated");
}
}
SQ.close();
}
(KEY_*** are column names that are defined elsewhere in the code)
I was wondering if this was the best approach, though. The code takes quite some time to run.
I have read somewhere that if you want to update data you can do this batch-wise. But can you do this as well when you want to update only if a record exists and when you want to add the record if it doesn't exits?
You can use a REPLACE INTO command as you would use an INSERT INTO one, in order not to have to check if the record exists and execute the corresponding UPDATE or INSERT INTO command.
SQLite will first delete the row (if existing) and then insert it newly.
Therefore you can write an execSQL() instruction which uses the REPLACE INTO command where you are currently executing a query and then deciding which command to execute next.
The REPLACE INTO will help you skipping all that logic.
I find it really useful.

sqlite android create unique column that no need to check availability when have to store

I developing news app that store all of news in sqlite database.
It will be a big database during time.
When i get data from API, check database to have every news and if doesn't exist store them to it. it takes 3-10 sec on every time when app run by user ( will take more during days ).
Is there anyway to store my data to database in an asynctask that doesn't freeze my app ? or another efficient way ?
every news has unique id. can i change my id column to unique that i don't have to check every time and store directly to database and database don't save it when it's available ?
for (int i = 0; i < jsonArrayAll.length(); i++)
if (!db.isNewsExist(news.getNews_id()))
db.addNews(news);
public boolean isNewsExist(String news_id) {
boolean exist;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor =
db.query("news", COLUMNS_NEWS, " news_id = ?",
new String[]{news_id}, // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit
if (!cursor.moveToFirst()) {
return false;
}
exist = cursor.getString(1) != null;
cursor.close();
db.close();
return exist;
}
Yes- just put the update code in the doInBackground of an AsyncTask. SQLite can handle being called from multiple threads. As for improvements- don't query and then add. Just try to insert- if the column has a unique constraint that's violated it will throw an exception you can catch and ignore.

Create a FIFO table with SQlite for Android application

I would like to create a FIFO table in order to save only the most 50 recent infomations by deleting the oldest elements when a new infomation arrives. I can do it by manipulating ID in the table but I don't think it is the best solution. Any idea of doing it well?
Instead of checking for date time, sorting your items, and whatnot, you can just assume that the first row in your table is the last to be inserted.
In your Content Provider's insert(Uri uri, ContentValues cv), before doing your db.insert call, you can first query the number of items on that table using getCount() and delete the first row if count>50. Then proceed with your insert call.
You dont need to play with IDs in order to create a FIFO logic. The best would be to add another column as DATETIME in your table which automatically inserts current time-stamp that will help you to select records in ascending order with respect to this column. Your new column should be something like:
DateAdded DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
Make sure when ever you insert new record, you must do a COUNT check of total records in this table and if necessary delete the oldest record with respect to DateAdded. Moreover, you can make use of LIMIT and/or MAX in your select-query when it comes to delete the oldest record.
Add a datetime type column to your table if it doesn't contain it yet and set it to 'now' on each insert. Then on each insert select all with limit set to 50 sorted by date. Choose the last item and run a delete query to delete everything older than this last item.
is it must to use sqlite? can you use file handling? you can use simple Queue object and save it to file.
Here is what I did for a list of transactions, and it works okay. When inserting a new entry I check if the count is above 50, if so, I just delete the very last entry:
// Adding new transaction
public void addTransaction(Transaction transaction) {
SQLiteDatabase db = this.getWritableDatabase();
if(getTransactionsCount() > 50){
List<Transaction> allTransactions = getAllTransactions();
Transaction oldestTransaction = allTransactions.get(allTransactions.size()-1);
deleteTransaction(oldestTransaction);
}
ContentValues values = new ContentValues();
values.put(KEY_TRANSACTION_UID, transaction.getUID());
values.put(KEY_TRANSACTION_AMOUNT, transaction.getAmount());
values.put(KEY_TRANSACTION_IS_ADD, transaction.getIsAdd());
// Inserting Row
db.insert(TABLE_TRANSACTIONS, null, values);
db.close(); // Closing database connection
}
And getAllTransactions() returns the list in descending order (based on the id primary key):
// Getting All Transactions
public List<Transaction> getAllTransactions() {
List<Transaction> transactionList = new ArrayList<Transaction>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_TRANSACTIONS + " ORDER BY " + KEY_TRANSACTION_ID + " DESC";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Transaction transaction = new Transaction();
transaction.setID(Integer.parseInt(cursor.getString(0)));
transaction.setUID(cursor.getString(1));
transaction.setAmount(cursor.getString(2));
transaction.setIsAdd(cursor.getString(3));
// Adding contact to list
transactionList.add(transaction);
} while (cursor.moveToNext());
}
// return contact list
return transactionList;
}

Android/SQLite: Insert-Update table columns to keep the identifier

Currently, I am using the following statement to create a table in an SQLite database on an Android device.
CREATE TABLE IF NOT EXISTS 'locations' (
'_id' INTEGER PRIMARY KEY AUTOINCREMENT, 'name' TEXT,
'latitude' REAL, 'longitude' REAL,
UNIQUE ( 'latitude', 'longitude' )
ON CONFLICT REPLACE );
The conflict-clause at the end causes that rows are dropped when new inserts are done that come with the same coordinates. The SQLite documentation contains further information about the conflict-clause.
Instead, I would like to keep the former rows and just update their columns. What is the most efficient way to do this in a Android/SQLite environment?
As a conflict-clause in the CREATE TABLE statement.
As an INSERT trigger.
As a conditional clause in the ContentProvider#insert method.
... any better you can think off
I would think it is more performant to handle such conflicts within the database. Also, I find it hard to rewrite the ContentProvider#insert method to consider the insert-update scenario. Here is code of the insert method:
public Uri insert(Uri uri, ContentValues values) {
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
long id = db.insert(DatabaseProperties.TABLE_NAME, null, values);
return ContentUris.withAppendedId(uri, id);
}
When data arrives from the backend all I do is inserting the data as follows.
getContentResolver.insert(CustomContract.Locations.CONTENT_URI, contentValues);
I have problems figuring out how to apply an alternative call to ContentProvider#update here. Additionally, this is not my favored solution anyways.
Edit:
#CommonsWare: I tried to implement your suggestion to use INSERT OR REPLACE. I came up with this ugly piece of code.
private static long insertOrReplace(SQLiteDatabase db, ContentValues values, String tableName) {
final String COMMA_SPACE = ", ";
StringBuilder columnsBuilder = new StringBuilder();
StringBuilder placeholdersBuilder = new StringBuilder();
List<Object> pureValues = new ArrayList<Object>(values.size());
Iterator<Entry<String, Object>> iterator = values.valueSet().iterator();
while (iterator.hasNext()) {
Entry<String, Object> pair = iterator.next();
String column = pair.getKey();
columnsBuilder.append(column).append(COMMA_SPACE);
placeholdersBuilder.append("?").append(COMMA_SPACE);
Object value = pair.getValue();
pureValues.add(value);
}
final String columns = columnsBuilder.substring(0, columnsBuilder.length() - COMMA_SPACE.length());
final String placeholders = placeholderBuilder.substring(0, placeholdersBuilder.length() - COMMA_SPACE.length());
db.execSQL("INSERT OR REPLACE INTO " + tableName + "(" + columns + ") VALUES (" + placeholders + ")", pureValues.toArray());
// The last insert id retrieved here is not safe. Some other inserts can happen inbetween.
Cursor cursor = db.rawQuery("SELECT * from SQLITE_SEQUENCE;", null);
long lastId = INVALID_LAST_ID;
if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
lastId = cursor.getLong(cursor.getColumnIndex("seq"));
}
cursor.close();
return lastId;
}
When I check the SQLite database, however, equal columns are still removed and inserted with new ids. I do not understand why this happens and thought the reason is my conflict-clause. But the documentation states the opposite.
The algorithm specified in the OR clause of an INSERT or UPDATE
overrides any algorithm specified in a CREATE TABLE. If no algorithm
is specified anywhere, the ABORT algorithm is used.
Another disadvantage of this attempt is that you loose the value of the id which is return by an insert statement. To compensate this, I finally found an option to ask for the last_insert_rowid. It is as explained in the posts of dtmilano and swiz. I am, however, not sure if this is safe since another insert can happen inbetween.
I can understand the perceived notion that it is best for performance to do all this logic in SQL, but perhaps the simplest (least code) solution is the best one in this case? Why not attempt the update first, and then use insertWithOnConflict() with CONFLICT_IGNORE to do the insert (if necessary) and get the row id you need:
public Uri insert(Uri uri, ContentValues values) {
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String selection = "latitude=? AND longitude=?";
String[] selectionArgs = new String[] {values.getAsString("latitude"),
values.getAsString("longitude")};
//Do an update if the constraints match
db.update(DatabaseProperties.TABLE_NAME, values, selection, null);
//This will return the id of the newly inserted row if no conflict
//It will also return the offending row without modifying it if in conflict
long id = db.insertWithOnConflict(DatabaseProperties.TABLE_NAME, null, values, CONFLICT_IGNORE);
return ContentUris.withAppendedId(uri, id);
}
A simpler solution would be to check the return value of update() and only do the insert if the affected count was zero, but then there would be a case where you could not obtain the id of the existing row without an additional select. This form of insert will always return to you the correct id to pass back in the Uri, and won't modify the database more than necessary.
If you want to do a large number of these at once, you might look at the bulkInsert() method on your provider, where you can run multiple inserts inside a single transaction. In this case, since you don't need to return the id of the updated record, the "simpler" solution should work just fine:
public int bulkInsert(Uri uri, ContentValues[] values) {
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String selection = "latitude=? AND longitude=?";
String[] selectionArgs = null;
int rowsAdded = 0;
long rowId;
db.beginTransaction();
try {
for (ContentValues cv : values) {
selectionArgs = new String[] {cv.getAsString("latitude"),
cv.getAsString("longitude")};
int affected = db.update(DatabaseProperties.TABLE_NAME,
cv, selection, selectionArgs);
if (affected == 0) {
rowId = db.insert(DatabaseProperties.TABLE_NAME, null, cv);
if (rowId > 0) rowsAdded++;
}
}
db.setTransactionSuccessful();
} catch (SQLException ex) {
Log.w(TAG, ex);
} finally {
db.endTransaction();
}
return rowsAdded;
}
In truth, the transaction code is what makes things faster by minimizing the number of times the database memory is written to the file, bulkInsert() just allows multiple ContentValues to be passed in with a single call to the provider.
One solution is to create a view for the locations table with a INSTEAD OF trigger on the view, then insert into the view. Here's what that would look like:
View:
CREATE VIEW locations_view AS SELECT * FROM locations;
Trigger:
CREATE TRIGGER update_location INSTEAD OF INSERT ON locations_view FOR EACH ROW
BEGIN
INSERT OR REPLACE INTO locations (_id, name, latitude, longitude) VALUES (
COALESCE(NEW._id,
(SELECT _id FROM locations WHERE latitude = NEW.latitude AND longitude = NEW.longitude)),
NEW.name,
NEW.latitude,
NEW.longitude
);
END;
Instead of inserting into the locations table, you insert into the locations_view view. The trigger will take care of providing the correct _id value by using the sub-select. If, for some reason, the insert already contains an _id the COALESCE will keep it and override an existing one in the table.
You'll probably want to check how much the sub-select affects performance and compare that to other possible changes you could make, but it does allow you keep this logic out of your code.
I tried some other solutions involving triggers on the table itself based on INSERT OR IGNORE, but it seems that BEFORE and AFTER triggers only trigger if it will actually insert into the table.
You might find this answer helpful, which is the basis for the trigger.
Edit: Due to BEFORE and AFTER triggers not firing when an insert is ignored (which could then have been updated instead), we need to rewrite the insert with an INSTEAD OF trigger. Unfortunately, those don't work with tables - we have to create a view to use it.
INSERT OR REPLACE works just like ON CONFLICT REPLACE. It will delete the row if the row with the unique column already exists and than it does an insert. It never does update.
I would recommend you stick with your current solution, you create table with ON CONFLICT clausule, but every time you insert a row and the constraint violation occurs, your new row will have new _id as origin row will be deleted.
Or you can create table without ON CONFLICT clausule and use INSERT OR REPLACE, you can use insertWithOnConflict() method for that, but it is available since API level 8, requires more coding and leads to the same solution as table with ON CONFLICT clausule.
If you still want to keep your origin row, it means you want to keep the same _id you will have to make two queries, first one for inserting a row, second to update a row if insertion failed (or vice versa). To preserve consistency, you have to execute queries in a transaction.
db.beginTransaction();
try {
long rowId = db.insert(table, null, values);
if (rowId == -1) {
// insertion failed
String whereClause = "latitude=? AND longitude=?";
String[] whereArgs = new String[] {values.getAsString("latitude"),
values.getAsString("longitude")};
db.update(table, values, whereClause, whereArgs);
// now you have to get rowId so you can return correct Uri from insert()
// method of your content provider, so another db.query() is required
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
Use insertWithOnConflict and set the last parameter (conflictAlgorithm) to CONFLICT_REPLACE.
Read more at the following links:
insertWithOnConflict documentation
CONFLICT_REPLACE flag
for me, none of the approaches are work if I don't have "_id"
you should first call update, if the affected rows are zero, then insert it with ignore:
String selection = MessageDetailTable.SMS_ID+" =?";
String[] selectionArgs = new String[] { String.valueOf(md.getSmsId())};
int affectedRows = db.update(MessageDetailTable.TABLE_NAME, values, selection,selectionArgs);
if(affectedRows<=0) {
long id = db.insertWithOnConflict(MessageDetailTable.TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_IGNORE);
}
Use INSERT OR REPLACE.
This is the correct way to do it.

Categories

Resources