-1

For my app, I need to have a database containing one table with 4 columns in it. This tables and its parameters will be static after creation, so that they will stay in the same place with the same data to be listed in a list view.

I have the DatabaseHandler for this purpose, but what I'm asking is how do I define this database in code? Does it build again every launch or is it only with the first launch? How does it work?

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • http://stackoverflow.com/questions/9109438/how-to-use-an-existing-database-with-an-android-application/9109728#9109728 – Yaqub Ahmad Nov 05 '12 at 16:28

2 Answers2

2

There are many ways of doing it. The one i follow is I will create database and tables in launch activity. Then i will insert data by counting the number of records in the table(Only for static table).So if(number of records == 0) then insert data into database. Otherwise do code for your app. It should work.

EDIT

This is the code to get total number of records in the database

In Database Class

YourDatabase

public class YourDatabase extends SQLiteOpenHelper{

   //coding for table create and insert records goes here
   //Your tables total number of records can be identified by following code
 public long yourTableCount()
 {
  SQLiteDatabase db = getReadableDatabase(); 
     return DatabaseUtils.queryNumEntries(db, YOURTABLE_NAME);  
 }
}

Your Activity

Calling Database class from your activity

YourDatabase db = new YourDatabase(this);

long numberofrecords = db.yourTableCount();
            if(numberofrecords == 0)
            {
                         //Insert your data in to database
                         //This will happen only in first launch because after that the     numberofrecords == total number of records inserted in the database.
                 }
vinothp
  • 9,939
  • 19
  • 61
  • 103
  • I'm sorry, I'm rather new. By records you mean? – user1772785 Nov 05 '12 at 16:49
  • Each row in a database is a record. Similarly, each column in a database is an attribute. – Ahmed Faisal Nov 05 '12 at 17:06
  • I'm sorry I think I still haven't gotten it. In your launch activity, say a splash screen, you're checking if records are 0 and then you insert data. Does this process happen every time, or is there a way to save the variable "records" for future launches? – user1772785 Nov 05 '12 at 17:31
  • @User177285 no it just check the condition every time you launch the activity. After first launch whenever you check the if condition it will return more than one. So the data won't insert every time your launch the activity – vinothp Nov 05 '12 at 18:16
  • I'm really really sorry but I'm not getting this clearly. Would you mind posting an example? I can't understand how to configure this condition properly, it just runs the same code each launch. – user1772785 Nov 05 '12 at 19:30
  • Thank you so much for your help and thanks for always checking if I replied :) – user1772785 Nov 06 '12 at 12:19
  • No problems. Happy coding.. All will learn at some point. If you happy with my answer then please mark it as answer. So others will find you got answer – vinothp Nov 06 '12 at 12:20
0

You can create the database manually using a database manager. Once you have the database defined in your assets folder it will remain there and be compressed within the apk file on build. Try SQLiteManager which has a free trial version that will let you design you database. Or use a firefox addon here: https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/

karmafunk
  • 1,453
  • 12
  • 20
  • Then later how do I define it in the code itself? You simply reference it and run some lines of SQL? – user1772785 Nov 05 '12 at 16:23
  • Yes, once you have created the database it resides as an slqite file. You then use the sqlite database handler class. There is a great little tutorial here. http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ – karmafunk Nov 06 '12 at 11:40