Android: SQLite Database. Do we need to create it? - android

I've been going through some tutorials on working with sqlite databases and they all seem to create a new database, tables, etc on the first run of the application. Is this necessary? What if I already have a pre-built database sitting in the assets folder when the application is installed? Can I not simply just open the connection to said database and start using its information or is there a specific reason everyone wants to create it using sql on first launch?

This question comes up frequently. Try this tutorial to use an existing database on Android:
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

You can't use database file which sits in assets folder directly as SQLite database, since this file would not be usual file located in common filesystem. E.g. you can have only readonly access to it. So the only your option is to copy those database from assets folder to device's filesystem.
To handle database creation for the first time and accessing it there's special helper class SQLiteOpenHelper. Read about it here. Specifically look in SQLiteOpenHelper.onCreate() - where should sit database creation (or copying from assets folder as in your case)

It's been quite sometime since I last worked with SQLite databases (in Android) but I believe that when they write CREATE statements, they always do so with the IF NOT EXISTS condition (i.e., CREATE (DATABASE|TABLE) IF NOT EXISTS...).
I don't know what you'll use SQLite for but I believe they do that in Android "just to make sure". That is, if it's the user's first time to run the app, the DB/Tables must be created first else app goes bonkers. Otherwise, they are (probably) created already and this case will be handled by the IF NOT EXISTS clause and they just go ahead and create a connection with the existing DB. Win-win.
(If, for some reason, it is not the user's first time to use the app and the DB isn't there, it will just be created again. But that's obvious isn't it? ;) )

If you just link with preexisting database it wont bind to your system. So there may be failures. Creating db at first run is the most appropriate way to work with db.

Related

Updating/Maintaining SQLite database after each App Release Xamarin Forms

This is my first time working on a Xamarin App and I am new to the app development world so I need some help figuring out this process.
Currently I run a php web service that generates some SQL files that I run in DB Browser and I get a database file which I then put into my Assets and Resources Folder. Using each platform's API I copy the database into a writable folder and use that to run my queries.
I followed this really helpful tutorial and it worked perfectly fine.
https://medium.com/#hameedkunkanoor/creating-a-sqlite-databse-and-storing-your-data-in-your-android-and-ios-application-in-xamarin-2ebaa79cdff0 .
After the "initial" setup I store a timestamp in a local table and and the next time the user opens the app I pass that timestamp and retrieve data that is older than that timestamp. The I update that timestamp and continue the process. That data is sent back in JSON format and make the updates to the tables.
My only concern is if a new version were to come out where I add a new table or a new column which is not present in the current version of my Database, how should I take care of those update Web Service calls? Is there a way of monitoring my DB version? I read somewhere where I could just ignore the new data that is not present already, like table or columns, but I'm not really sure how to do that.
I also saw that if I call CreateTable on my current tables I could potentially update them?
Also for future reference each time I develop a new app would I need to regenerate a new database file to store in the assets/resources folder? Is there a more automated process for this? Along with monitoring the version of my database?
Any Help/Tutorials/Suggestions would be greatly appreciated.
You have to remember that CreateTable it's already doing the columns update for you, because internally it calls a method called MigrateTable which you can see here for further clarification: https://github.com/praeclarum/sqlite-net/blob/master/src/SQLite.cs#L562.
However you could have to handle more advanced modification to your database, like adding triggers or something similar.
In that case i suggest you to perform modifications manually.
In Xamarin Forms i've ended up with this:
https://gist.github.com/matpag/b2545cc22c8e22449cd7eaf6b4910396
Could not be the best strategy ever but seems to work for me.
Summarizing :
You have to save the database version in an internal flag of the SQlite database called user_version accessible with PRAGMA keyword.
Every time you get the database connection, you have to perform a check and see if the current database version is the same as the app last database version.
If not you need to perform a database update and set the new current version.
Reference here.

No credentials needed for Sqlite database?

I am not quite getting the idea of sqlite database. I am developing a hybrid mobile app using Ionic Framework and backend with PHP.
I referrred to this article for using the sqlite https://blog.nraboy.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/
I have a few questions now,
1) So, anybody can create a table,insert and do any kind of operations? Is'nt there any need of a security method to access the DB like mysql?
2) Is a separate instance of Sqlite created for each application? Coz if not,I can make a guess for a tablename created by any other application and delete it all together,isn't it? I just dont get it?
1) SQLite is a file database so if you have access to file you have access to database and can do anything with it. What is the purpose of restricting access to db when you can just delete whole file? SQLite db file should be created in app private storage so no one have access to it only your app.
2) the answear is above. just create file in right place and everything will be fine :)
1) So, anybody can create a table,insert and do any kind of operations? Is'nt there any need of a security method to access the DB like mysql?
By default, each app db will store in their private folder(data/data/your_package_name/database and only that app or system app can access to it.
But if you choose to store your db in sdcard, anybody can mess up your db if they know the path to db file and have right to access it.
Other case is if your device is root, other apps can access your db too.
2) Is a separate instance of Sqlite created for each application? Coz if not,I can make a guess for a tablename created by any other application and delete it all together,isn't it? I just dont get it?
Like above, each app can choose where to store their db file, so basically, they a separate instance.
You can also create multi db file for one app if you like.

android: is it possible to check if db is created in other app

Is it possible to check if a db is created in other app? I have to have a common db for two app. If one app is already created the db then the second one should use the first apps' db (I mean should not create new one). For this,Is there a way to check whether that particular common db is created or not? Please help me on this.
This is not as easy as you may think. Even If you would definetly know, that your other app has created the db already, there is still difficulty in querying it. As any Db belongs to the app which created it, only this app usually gets access to it.
Good news is that there is is a mechanism for that, called ContentProvider. With it you are able to share the db-infos between apps, but it takes some effort to implement this.
The database is stored under /data/data/your.applications.package/databases. Normally this location could only be access by the user the Android OS created for the app. No other user is able to access this location unless the device is rooted. Then any user can access any location on the phone and manipulate the data.
so you can check the whether the db is present in this location or not
you can directly check this Android Access Another App's Database
The best thing for you is to use the sdcard (or some other user accessible area, without root)
Look at this thread
I know it's unsecure, but it's the best thing for this kind of problem. One application (the first one installed) creates the db on sdcard. The other one, just check's if the file (db) allready exists and reads from the existing db.
You should use SQLiteOpenHelper to control versioning of your database as described in database tutorial.
Unfortunately SQLiteOpenHelper only works in local app-directory but you can tell it to use a different directory on sd-card as described in sqliteopenhelper-with-fully-qualified-db-path-name
Try the following code
DB_PATH = "/data/data/THE_PACKAGE_NAME/databases/";
DB_NAME="YOUR_DB_NAME";
File dbFile = new File(DB_PATH + DB_NAME);
if(dbFile.exists()){
/* YOUR CODE HERE*/
}
Do the above check in both of your application with the PACKAGE NAME before creating the db.
:)

Updgrading database downloading it from a server

I have an app that creates a database and do some stuff. I am wondering if i upload a new db to a server and download it to the exact folder where the older one exists it will be overwritten and i am good to go? Or there will be a problem. Assuming it has the same name, same column names, etc. Of course i am reffering to sqlite.
In Android, when performing a database update you should be using onUpgrade inside of the SQLiteOpenHelper. One way of doing this is to download text files that include the sql instructions needed to modify the current database or update rows with new data. The reason you have to do this is because Android will only create the database once. After the initial creation the call to onCreate for the database will not occur.

Sqlite database lifecycle? It is deleted when the app is closed?

I'm following a simple tutorial that creates a class which extends from SQLiteOpenHelper and creates a DB with one table and 5 rows.
OK, but I need to understand some more about android Sqlite databases. For example, what happens if the app is closed or the phone is off? Is the database deleted?
Of course the database isn't deleted. I assume you're doing it the "proper" way. In which case the database is persistent. (of course if you choose to create a database in a temporary directory or something similar then its not going to work properly).
Think of it like this. The database is basically a text file. What you're doing to the database is modifying the contents of that text file (ok its a little bit more complicated in real life, but its a good way to think about it).
Once you've made a change to the database (e.g. added a row) the database file is saved onto the disk thus persisting it. If the phone is turned off or the app is quit then the database file persists and you can keep connecting to it in the future.
The database is deleted only when your app is deleted, the user clears the data associated with it or you do it programmatically.
Therefore, your app can be killed or the phone rebooted and your database persists. That's why database is considered to be a persistent storage.
what happens if the app is closed or the phone is off?
Answer is No, database not deleted, your data is only deleted when you Uninstall the Application or Clear data from Application->Manage Application->Application_Name from your device.
When a database is created it lives in your applications private file store and is only deleted when you explicitly delete it (using Context.deleteDatabase) or when your application is uninstalled.

Categories

Resources