Checking new row is inserted in table database and run some script - android

I want to send some notification to android app when new row is inserted in table database, but I'm having trouble to check if there is new row inserted or not.
Is there any function or query to check if new row inserted in table? or some logic to acquire that.

It depends on the database you are using.
For example in MySQL you can query the table information_schema.tables to get the latest update time.
https://dev.mysql.com/doc/refman/8.0/en/information-schema-tables-table.html
For postgres
Check if track commit timestamp is on
show track_commit_timestamp;
If it's off, set it to on in your postgresql.conf file and restart postgres.
Post that run the following to track the latest updates in your table
SELECT pg_xact_commit_timestamp(xmin), * FROM YOUR_TABLE_NAME;

Related

Update Android database from CSV file

I'm developing an application which would download the CSV file and populate the DB table with the CSV's data.
The problem is that CSV file will be modified every hour and my app will have to update the DB according to the changes.
I don't know which the changes will be - a row is deleted/added or a record is changed.
What will be the best practice in this case? Should I drop the table and create it again when the new CSV is downloaded? Or will it better to compare existing values with the new ones and make the necessary changes?
I think your problem can be solved if you put an updateTime column in your table and when you import data from csv everytime, you just perform the update with:
int rowsEffected = database.update(...);
if rowsEffected>0, you perform insert. Everytime you insert or update you need to put the updated datetime in the updateTime column and also in a sharedpreference. Next, when you get the data from the csv file next hour, just check the datetime from sharedpreference and delete every column that is not marked with the datatime in the updateTime column, as they were not present in the previous csv and continue the process of insert and update. I would not drop and recreate because drop and recreate resembles to clean start but here you are just manipulating data.

Update SQLite table of Android application optimally

I am working on an android application using android:minSdkVersion="14". The application receives data as JSON from a server. The data received need to be added to an sqlite table. If a row exists, all fields except for two have to be updated. If a row does not already exist in the table, it has to be inserted. I am looking for the most efficient way as regards performance.
The function insertwithonCoflict() has been considered but it is not an option since in case of update, it updates all the fields including the two that should not be updated.
The function replace() is also not suitable.
I would opt for a SELECT to check if the row exists and then an INSERT or UPDATE but I was wondering if I could optimize the procedure somehow .
Two approaches:
Change the database structure so that the table has only the server data. Put local data (the two columns) in another table that references the server data table. When updating, just insert to the server data table with "replace" conflict resolution.
Do the select-insert/update logic.
For performance in any case, use database transactions to reduce I/O. That is, wrap the database update loop in a transaction and only commit it when you've done with everything. (In case the transaction becomes too large, split the loop into transaction chunks of maybe a few thousand rows.)
A nice solution I use is as follows:
long id = db.insertWithOnConflict(TABLE, null, contentValues, SQLiteDatabase.CONFLICT_IGNORE);
if(id!=-1) db.update(TABLE, contentValues, "_id=?", new String[]{String.valueOf(id)});
This ensures the row exists and has the latest values.

Android and automagically updated databases

I write app on android which will use android SQLite database. I have table which will contain data rows with unique Id. From time to time I need to add new row or update old row. For now I when I want to add/update data I must check if I have row with specified Id in table. If I have I update suitable row. If I dont I create new row with new unique key. My question is: is there something in Android what can automagically check if in database is row with specified id and then just update it and if there isnt such row add new?
Check out the SQLite documentation for the insert statement. You can use a insert or replace statement (or just replace, which is a shortcut).
You use it just like you would a traditional insert statement. This is probably useful in your case since you already have the key known.

"In place" update in Android SQLite database not working with rawQuery

I need to execute an "in place" update SQL Query on an SQLite database in my android application. The query is of the form:
update table T set column = column + 1 where someOtherColumn = aValue
I tried executing this query with SQLiteDatabase.rawQuery(updateQuery, "someOtherColumn = ?", new String{}[aValue]) but I find that the column is not incremented. When I run the same query with the usual SQLiteDatabase.update(...) it works fine, but this requires me to fetch the old column value, update it outside and then execute update() which is probably less efficient since it requires a select and then an update.
Is there some way I can update "in place" by using the first query?
Thanks.
Is there some way I can update "in place" by using the first query?
That's not a query. It's an UPDATE statement. It does not return a result set. Use it with execSQL(), not rawQuery().
but this requires me to fetch the old column value, update it outside and then execute update() which is probably less efficient since it requires a select and then an update
This is the way all SQL databases work. Now, those that offer stored procedures may be able to let you do an UPDATE and a SELECT in one request, but it is still an UPDATE and a SELECT. Furthermore, SQLite does not support stored procedures, at least when I last looked.

How to efficiently sync Android database with web database

My android app database syncs with a central database on my web server. My problem is efficiently syncing it.
The table is set up as "id" (primary key), "town".
The android app wants all the ids of the people in a given town
My current process is:
get list of all ids for town "A"
set all existing records in Android database to "old"
add/update ids in the list to "current"
delete any records still set as "old"
The bottle neck comes in the add/update process which is:
for each id {
result = update WHERE _id = id
if (result == 0) {
add record
}
}
Is there a statement like WHERE _id IN(id1, id2, id3, id4 ....) that would act as add/update if already there??
Or am I going about it completely wrong?
Thanks
then why you updating the data, at the time of synch cant you delete all the data in android device and add the latest data from the server
i am saying this because it will flexible, first delete all and insert all(latest ones)
build a table on server side (ID, Town, UpdateTime)
add triger on UPDATE/INSERT to modify UpdateTime
store LastUpdate time on device with fx SharedPreferences
send LastUpdate to server
build JSON or XML with ID, Town from a table WHERE UpdateTime > LastUpdate +(place in JSON or XML) (CurrentTime GETDATE()
from DB)
do insert/updates in device db update, LastUpdate to CurrentTime

Categories

Resources