Android: SQLite database. Does it need to be created?

I have studied some sqlite database tutorials and they all seem to create a new database, tables, etc. in the first launch of the application. It's necessary? What should I do if I already have a ready-made database sitting in the asset folder when installing the application? Can I just simply open a connection to the specified database and start using its information, or is there a specific reason that everyone wants to create using sql on first start?

+4
source share
4 answers

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

+2
source
  • You cannot use the database file located in the resource folder directly in the SQLite database, since this file would not be a regular file located in the shared file system. For instance. you can only have read-only access to it. Thus, the only option is to copy this database from the file folder to the device’s file system.
  • To process the creation of the database for the first time and access it a special SQLiteOpenHelper helper SQLiteOpenHelper . Read about it here . In particular, look at SQLiteOpenHelper.onCreate() - where database creation should be created (or copying from a data folder as in your case)
+1
source

Most recently, I worked with SQLite databases (on Android), but I think that when they write CREATE statements, they always do this with the IF NOT EXISTS clause (i.e. CREATE (DATABASE|TABLE) IF NOT EXISTS... ) .

I don’t know what you will use SQLite for, but I believe that they do it in Android “just to make sure”. That is, if this is the first time the user launches the application, DB / Tables should be created first, the application goes to bonkers. Otherwise, they are (probably) already created, and this case will be handled with the IF NOT EXISTS clause, and they just go ahead and create a connection to the existing database. Win-win.

(If for some reason the user is not using the application for the first time, and the database is not there, it will simply be created again. But this is obvious, isn't it ?;))

0
source

If you simply connect to an existing database, it does not bind to your system. Therefore, there may be glitches. Creating db on first run is the most appropriate way to work with db.

0
source

All Articles