How to prevent multiple databases from being created within Android app?

I'm relatively new to Android programming and I have little to no experience with SQL and its interaction with Java altogether, but I couldn't find the answer I was looking for either in tutorials or Google's developer pages.

I'm wondering how I can create a database in Android to store a variety of String and int variables without creating duplicate databases. I understand the schema and contract class concept presented by the development page, but I don't understand how I am supposed to call a class method so that only a single database is created (preferably at first app launch) without several instances being created.

My main question: What sort of setup is recommended to create a database that can be referenced from other classes, exists from the very first launch of the app, and remains private (although to be honest it's not a huge priority)?

Answers


but I don't understand how I am supposed to call a class method so that only a single database is created (preferably at first app launch)

In Android, database is created only once and then whenever you will try to perform CRUD operations and selects statements, always you will have only one database created. It's same for table(s) in database. Once table(s) are created, they won't be created again (so be careful if you'll add new columns to table during implementation if table was created before without new columns1). Moreover if user explicitly will delete application data - only in this case, database and table(s) will be created again because they were deleted.

What sort of setup is recommended to create a database that can be referenced from other classes, exists from the very first launch of the app

In the moment when you'll call for example getWriteableDatabase() or getReadableDatabase() database and table(s) will be created - only at this time! Only once. No more (except1. After that, whenever you'll try to attempt to read or write from / to database your database will be opened with specific permissions (read-write or read-only - it depends how which method you called) and you can perform actions.

without several instances being created.

Database is only one - it's not duplicated (physically), absolutely but when application logic is not designated correctly, there may appear multiple instances of database and this fact is not very good and usually a nightmare of many beginners with Android and SQLite.

But solution exists and it's not hard to implement -> Singleton. Design pattern Singleton is "pretty cool tool" for our goal - ensure that only one instance of database will be ever created.

Here is implementation of Singleton itself:

public class DataSource extends SQLiteOpenHelper {

   private static DataSource instance;

   public static final DataSource getInstance(Context c) {
      if (instance == null) {
         instance = new DataSource(c);
      }
      return instance;
   }
}

1 If you added new columns to table at the time when database and table were created before (how i mentioned above the're created only once) it may cause application exception so exactly here you have to clear application data or implement onUpgrade() method.


Try using an ORM like ActiveAndroid. While not recommended for extremely complex apps, using an ORM will make using and managing a database much easier for someone without much database experience (and can make your code more readable as well).


When I did my first SQLite DB in Android, I used this site to get me trough the basics.

To answer your question, take a look at the DatabaseHelper Class. You will see that you have to specify a DB name (filename).

private static final String DATABASE_NAME = "commments.db";

Android will automatically create the DB if it's not existent (newly installed app) and will reuse it if it's there. As long as you use always the same file (and don't mess up the tables), your DB will remain intact.

Also, for security, the DB is stored in the app's data. Not accessible with a file browser (unless you are rooted). You can download the DB via adb if you really need to. There is tons of documentation on how to gets apps private data.

Hope this helps!


Need Your Help

Animate on scroll in mobile safari

jquery css3 mobile css-transitions fixed

I have a header with position:fixed and inside it I have a logo. The header gets added a class "small" on scroll and changes the logo from a large image to small image and uses CSS3 transitions to ...

Solr port disable when connecting to remote server through local machine

solr localhost port telnet remote-server

I have installed Solr on remote server and started on port 8983. Now, I have bind my local machine port 8983 with remote server 8983 of Solr using ssh (Ubuntu OS). When I am requesting on Solr for