Comparing SQLite databases on Android - java

In my application I have an existing SQLite database attached. On start it's copied to phone and then being used. I know how to check if this database is already copied, but I want to check if database on the phone is the same as stored in APK. Is there any way to compare them? I want to do that programmatically.
Original database is stored in assets folder.

If you have a rooted Phone then you can Watch the Details of It. but if your phone is not rooted then you are won't be able to see it.
Another thing if you have copied the Application in your Phone then it will definitely work.
So Conclusion is that If you have a Rooted Phone then you will be able to See and Compare, if possible try to compare its size or Time on which it is Installed.

If you have attach a DataBase with your APK and you successfully copied to your phone that exactly means that you have your old data base copied.So i think no need to compare.If you want to compare then compare all record in Table.
Second if you want to remember that your data base is already copied then use one flag in share preferences.Save true once copied.Then check its value
Updated
To listing all Table
select name from sqlite_master where type = 'table'
If both data base have same table then compare record of these corresponding Table

Related

Can i replace my table-arranged dummy values with actual data froma database?

[So as you can see in the picture, i'm a newbie in android programming and i just had an assignment concerning creating a management system app for university.
The views you see in the picture are just customly made with xml(Table layout) and the data are just dummy data placed on the respective textviews in the tables. I desire to create a database for students and all those data e.g names, gender, should be fetched from the database or sent. Remember, as for courses the number of courses would differ from one degree prog to another(imagine a student with less ormore courses than the tables i sampled there).
How do i acomplish this
As for database, im thinking of using SQLite or Firebase ]1
There are many ways to approach this, depending on the requirements of your program. If you need to share data between multiple people or devices, you probably want a database that lives on a web server, and you would write an API (application programming interface) which you would call from your app, and it would go and get the data from the database and return it to your app.
If your app is meant to be 'stand alone', where you could use it without being connected to the internet, then yes, a SQLite database is a good solution and it is pretty easy to integrate into your app.
To create a SQLite database, you can download the free tool SQLiteStudio. Once you create the database (usually saved with a .db file extension) you can add it to your project in the Assets folder (create it if it doesn't exist). The folder lives at the same level as the bin and gen folders in your project.
Next, search for a class called DatabaseHelper.java, it has a number of methods for opening, closing and querying a SQLite database, copy that to your project.
At the start of your program, you should check to see if you have a copy of the database in your local data/databases folder in your App's file storage area. If you don't have a copy there, which on the first execution of your program, it will not be there, then you have to copy the database .db file from your Assets folder to the data/databases folder, then open it there. If it is found, you've already done this, so, just open it.
When you want to read data into your app, you execute a SELECT statement to retrieve data from your database into a Cursor object, and return that cursor from your DatabaseHelper class to your activity. In your activity, you will iterate through the Cursor, reading one row at a time, and copy the data into program variables. Sometimes, you will create a class object to hold one record, with attributes that match a row from your query, and you'll create an instance of your class, fill it with the data from the Cursor, then add that object to an Array List of objects which your program can use at a later time, say, to display a list of people or whatever it is you queried for. Once the Cursor has been read to the end, you close the Database by calling a close method in the DatabaseHelper class.
That's the general idea. If you search for a copy of the DatabaseHelper.java class, that will get you started. Then, search for some example projects that use that DatabaseHelper.java class so you can figure out how to use it.
Good luck!!

How can I keep track which person has added which data to a database?

I have an app where anyone who downloads the app can add data to the database. The data THEY added will get displayed in a listView for only them to see. The users don't have to register an account or anything. Now when multiple people use the app, different data gets added to the database. So my question is, what are good ways to differentiate data, so the person who added data, only sees the data they added.
I have two ideas, either add the data ID to sharedpreferences, so when I select the data from the database, I select data where the data ID equals to the one in sharedpreferences OR when I add the data to the database, I add a unique key, so I can select * where unique key equals x.
I like the second idea more, but what would be a good unique key to use. I've thought of using
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
but is that a reliable solution, are there any other unique key solutions I could use?
Any feedback is much appreciated!
I think that the best way to handle such cases is to generate a UUID for each user.
Android itself will help you generate one.
You can generate the UUID the first time the app is launched, and store id in SharedPreferences.

Does it Create a new database in every time when you run the app?

Based on these tutorial about creating a database in android in relation to SQLite.
https://www.youtube.com/watch?v=cp2rL3sAFmI
https://www.youtube.com/watch?v=p8TaTgr4uKM
My question is:
WHen you run this app and it creates a new database and you save some data. However when you rerun the application, does it create a new database again? What would be happened to the previous saved data? will they be removed?
Today Im a newbie in android
Thank you!
The SQLiteOpenHelper will call the different lifecycle methods based on the version number that you pass in via its constructor. It will compare the last version number that you passed in.
SQLiteOpenHelper#onCreate is called only when there is no database available that the location specified. This is where you create your tables.
SQLiteOpenHelper#onUpgrade is called only when there is a database available and the version number is higher than the previous version number. This is where you will do SQL operations to update the database tables to what they should be for the new schema. Most tutorials generally show a clear tables operation and a manual call to onCreate(). If you do that, then you will wipe the entire database every time you upgrade it (if you increase the version number).
SQLiteOpenHelper#onDowngrade is called only when there is a database available and the version number is lower than the previous version number. Similar to onUpgrade, this is where you would do SQL operations to revert the tables to what they should be.

Possible overwriting to database table

Let's say you have a database table name table1 with columns name and surname. Two different clients open the same view from the java application and get the data for same person at the same time.
1) First client changed the name and pressed save button to update database record.
2) Client2 still sees the old record on the screen and then pressed to save button to change the surname.
It actually overwrite the record by old name. I think to check and get the latest data before updating the database when I click button but I do not like this solution because of making a second query before update.
So how can we solve this problem by using Hibernate and without using Hibarnete. Any idea?
Of course if you do not want that something will be overridden, then you have to check the data before an update. But it will be not always a real query with a good caching strategy. You could also use a timestamp with last update to compare it easier. Another strategy would be to lock the entities when the first user will read them. But that is normally not a good design for web applications or you have to integrate a messaging service, which will all user inform for an update who actually have open that entity. But I think that is not so easy to implement and a more advanced feature.
In short, compare the timestamp of an entity and if already updated, then compare the changes and show them for the user who wanted update that entity.

add new column in default contacts content provider of android

I have requirement to add new column in default contacts table provided by android so can I do that .I just want to add a new value in that column if it is added from my application.So please tell me if it is possible .
Edit:My aim is to add data in defaults contacts table programatically so that it will also be visible when user opens default phonebook.
No. This is not possible.
Though it technically is, the end result will not be desirable:
It would need root obviously. You can't modify the data of other apps without root.
You would need to find where the actual SQLite database file is located, open that, modify it.
Would the contacts app even work after this modification? Doubtful.
Would this modification persist? Doubtful.
Why not make the association some other way? Store whatever value you have with an identifuer from the contacts database.

Categories