Update Android database from CSV file - android

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.

Related

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

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;

Android: SqliteDatabase's lastModified() is giving wrong value

I am using sqlcipher database. I am tracking the lastModified time of my database. According to my understanding long value returned by lastModified() function will change only if we update or add a value to the database we refer. I am using a query to fetch (not modifying) a value from the database, for this i am using the below code
mDatabaseFileObj = mContext.getDatabasePath("xxx.db");
Log.i(""," "+mDatabaseFileObj.lastModified());
mSQLiteDatabase = net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(...)
Log.i(""," "+mDatabaseFileObj.lastModified());
mCursor = mSQLiteDatabase.rawQuery(query, null);
do{
....
}while(..)
In this i had printed two logs. First log before creation of mSQLiteDatabase obj and another log after that.According to the doc for lastModified() both the values printed by the logs should be same as i just quering not modifying the database. But the value is changing.
I couldnt sort out this problem.Give your thoughts on this.
An addtional info is, i had placed this code snippet in a function and i am calling that function 5 times and strangely for the first time alone the log is printing different values but for the rest 4 times the log printed values are same..
Thanks in Advance
Deepak,
openOrCreateDatabase is not a read only operation. In particular the wrapping library, which is based on the Android sqlite library, manipulates a table called android_metadata when the database is open. This could cause the timestamp to change, because the database is actually modified during open.
mDatabaseFileObj this is reference to your File object from OS don't confuse this with database in SQLITE database are implemented on top of file system only, so in first line you are printing when this file was last modified,
while second line you are trying to alter the file, and third line again printing time, so as per me and going with file systemn behaviour you will get a different time stamp, this doesn't mean if content inside this file was modified or not.
Just imagine it like this, open a txt file in windows and save it again without changing it notice time before and after they will be different.
Hope this help.

How can I recreate a database with default values?

In my application, I want to delete my existing database and create a new one with default values. Default values can be inserted to the database from XML.
Does anyone have any idea on how to reuse a database?
Assuming that you are using a SQLite database, to delete all the rows from all of your tables inside your database you can use db.execSQL() and heed the advice from this question Drop all tables command:
You can do it with the following DANGEROUS commands:
PRAGMA writable_schema = 1;
delete from sqlite_master where type = 'table';
PRAGMA writable_schema = 0;
you then want to recover the deleted space with
VACUUM
and a good test to make sure everything is ok
PRAGMA INTEGRITY_CHECK;
If you haven't written a way to read your XML data yet, this is excellent reading: Store parsed xml data to sqlite ? Android
Well basically that's not an Android specific question.
Firstly when do you want to recreate the database with default values and how to trigget it.
In an UI event like button click etc?
Or when you start/stop or destroy your activity?
In any cases you need to drop the database and recreate the whole structure (tables,relationships etc.) again.

Changing row data in SQLite table, without deleting and re-inserting updated values again

I made SQLite database table that stores records, is it possible to change some values
that stored inside the table without deleting and entering updated values with older one.
e.g.
Lets say I want to change the wage of row ID = 0 to from 23$ to 40$ without deleting
the first row and reinserting making the auto incremented ID change to 1,
is to possible?!
Yes, you should use an UPDATE statement.
Yes, check out the UPDATE syntax of SQLite.

What primary key to use in my SQLite database?

I have a .csv file that I turned into an SQLite database with the first column as a date and time (as a TEXT datatype because it's in the format 7/20/2011 12:00:00 PM), the second column as the subsecond (a REAL, like 0.142857), and the rest of the columns as data about that time. I will be using it as a read-only db in my Android app, in which the user selects a start time and an end time and then the data is displayed graphically for that time period.
This is my first time using a database and I'm not sure how to work with this. I believe I need a unique NUMERIC primary key for each row but simply labeling them by row number is a waste of space. The first column is not unique and is a TEXT datatype anyway, though if the subsecond was to be somehow incorporated then it would be unique. But I really can't re-write the database because it has 65534 rows... How do you suggest I access specific times in this database?
In Android you need a column named _ID in your database (else you'll face some issues later on). You will use that as the primary key.
Dates and times can be stored in SQLite in the form of a text column in the following format (See http://www.sqlite.org/datatype3.html):
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS")
If your database is static, simply use a tool such as SQLite Database Browser to convert it to a format convenient for Android.
If your database is local and external(not remote), than you must have _id and another another table android_metadata which will hold the locale.
If your database was remote. Yes, you can it is only matter of speed if you are write, since you don't. Using WHERE clause will do the work.
Every date can be converted to numeric timestamp quite easy:
Date date = new Date();
long timestamp = date.getTime();
Date otherDate = new Date(timestamp);
Numbers are MUCH easier and faster to process than text fields. If you are completely sure, that you have unique data within column you can use it as primary key.
Importing csv file into table should be also easy:
//just open file in some known way and read it line by line
// we have declared String[] data, String line is line read from your csv somewhere earlier
// in code
data = line.split(",");
//here you have to process values, and execute insert statement
You have to create indexes on every column which will be used to search or order data. Please be also aware, that rows in table has no "default", "natural" or any other order. If you execute this same select statement twice you can get two totally different results in meaning of sorting. So simple select statement should look like that:
select
_id, other_colum_name, some_other_column
from
table_name
where
column_name = 5
and other_column_name = 3
order by
column_name;

Categories

Resources