Creating a database that is Permenantly Stored on Device

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Creating a database that is Permenantly Stored on Device

Postby Winner134 » Mon Aug 24, 2009 12:01 am

Hi,

I have created a database that has a table of food items and their corresponding calorie values. The full detail of the table is as follows



Food Item Food Amount Unit of Amount Calorie Value of Food Amount


"Almonds, shelled", 1, "Item", 11
"Apple", 1, "Item", 80
"Apple juice", 240, "ml", 115 -------> 115 Cal / 240 ml
"Barley", 113.4, "g", 390

To create the database, I have created a class called DBAdapter. The following is the code for it:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class DBAdapter
  3. {
  4.     public static final String KEY_ROWID = "_id";
  5.     public static final String KEY_ITEM = "Item";
  6.     public static final String KEY_AMOUNT = "Amount";
  7.     public static final String KEY_UNIT = "Unit";  
  8.     public static final String KEY_CALORIE_VALUE = "CalorieValue";    
  9.     private static final String TAG = "DBAdapter";
  10.    
  11.     private static final String DATABASE_NAME = "nutrition";
  12.     private static final String DATABASE_TABLE = "foods";
  13.     private static final int DATABASE_VERSION = 1;
  14.  
  15.     private static final String DATABASE_CREATE =
  16.         "create table foods (_id integer primary key autoincrement, "
  17.         + "Item text not null," + "Amount double not null," +  "Unit text not null,"
  18.         + "CalorieValue integer not null);";
  19.        
  20.     private final Context context;
  21.    
  22.     private DatabaseHelper DBHelper;
  23.     private SQLiteDatabase db;
  24.    
  25.     public DBAdapter(Context ctx)
  26.     {
  27.         this.context = ctx;
  28.         DBHelper = new DatabaseHelper(context);
  29.     }
  30.        
  31.     private static class DatabaseHelper extends SQLiteOpenHelper
  32.     {
  33.         DatabaseHelper(Context context)
  34.         {
  35.             super(context, DATABASE_NAME, null, DATABASE_VERSION);
  36.         }
  37.  
  38.         @Override
  39.         public void onCreate(SQLiteDatabase db)
  40.         {
  41.            
  42.                 db.execSQL(DATABASE_CREATE);
  43.         }
  44.  
  45.         @Override
  46.         public void onUpgrade(SQLiteDatabase db, int oldVersion,
  47.         int newVersion)
  48.         {
  49.             Log.w(TAG, "Upgrading database from version " + oldVersion
  50.                     + " to "
  51.                     + newVersion + ", which will destroy all old data");
  52.             db.execSQL("DROP TABLE IF EXISTS foods");
  53.             onCreate(db);
  54.         }
  55.     }    
  56.    
  57.     //---opens the database---
  58.     public DBAdapter open() throws SQLException
  59.     {
  60.         db = DBHelper.getWritableDatabase();
  61.         return this;
  62.     }
  63.  
  64.     //---closes the database---    
  65.     public void close()
  66.     {
  67.         DBHelper.close();
  68.     }
  69.    
  70.     //---insert a title into the database---
  71.     public long insertTitle(String item, double amount, String unit, int calorieValue)
  72.     {
  73.         ContentValues initialValues = new ContentValues();
  74.         initialValues.put( KEY_ITEM , item);
  75.         initialValues.put(KEY_AMOUNT, amount);
  76.         initialValues.put(KEY_UNIT, unit);
  77.         initialValues.put( KEY_CALORIE_VALUE , calorieValue);
  78.         return db.insert(DATABASE_TABLE, null, initialValues);
  79.     }
  80.  
  81.     //---deletes a particular title---
  82.     public boolean deleteTitle(long rowId)
  83.     {
  84.         return db.delete(DATABASE_TABLE, KEY_ROWID +
  85.                         "=" + rowId, null) > 0;
  86.     }
  87.  
  88.     //---retrieves all the titles---
  89.     public Cursor getAllTitles()
  90.     {
  91.         return db.query(DATABASE_TABLE, new String[] {
  92.                         KEY_ROWID,
  93.                         KEY_ITEM,
  94.                         KEY_AMOUNT,
  95.                         KEY_UNIT,
  96.                         KEY_CALORIE_VALUE},
  97.                 null,
  98.                 null,
  99.                 null,
  100.                 null,
  101.                 null);
  102.     }
  103.  
  104.     //---retrieves a particular title---
  105.     public Cursor getTitle(long rowId) throws SQLException
  106.     {
  107.         Cursor mCursor =
  108.                 db.query(true, DATABASE_TABLE, new String[] {
  109.                                 KEY_ROWID,
  110.                                 KEY_ITEM,
  111.                                 KEY_AMOUNT,
  112.                                 KEY_UNIT,
  113.                                 KEY_CALORIE_VALUE
  114.                                 },
  115.                                 KEY_ROWID + "=" + rowId,
  116.                                 null,
  117.                                 null,
  118.                                 null,
  119.                                 null,
  120.                                 null);
  121.         if (mCursor != null) {
  122.             mCursor.moveToFirst();
  123.         }
  124.         return mCursor;
  125.     }
  126.  
  127.     //---updates a title---
  128.     public boolean updateTitle(long rowId, String item, double amount, String unit, int calorieValue)
  129.     {
  130.         ContentValues args = new ContentValues();
  131.         args.put( KEY_ITEM , item);
  132.         args.put(KEY_AMOUNT, amount);
  133.         args.put(KEY_UNIT, unit);
  134.         args.put( KEY_CALORIE_VALUE , calorieValue);
  135.         return db.update(DATABASE_TABLE, args,
  136.                          KEY_ROWID + "=" + rowId, null) > 0;
  137.     }
  138. }
  139.  
  140.  
Parsed in 0.048 seconds, using GeSHi 1.0.8.4


So to create the database, I just create a new instance of DBAdapter and use insertTitle() method to insert rows into the table like so:

I put this code inside the onCreate() method of my activity class

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onCreate(Bundle savedInstanceState) {
  2.  
  3. DBAdapter db = new DBAdapter(this);
  4. db.open();
  5. db.insertTitle("Almonds, shelled", 1, "Item", 11);
  6. db.insertTitle("Apple", 1,      "Item", 80);
  7. db.insertTitle("Apple juice", 240, "ml", 115);
  8.  
  9. // More code here (that does not concern this post)
  10.  
  11.  
  12. db.close();
  13.  
  14. }
  15.  
  16.  
  17.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4



So this way every time the onCreate method runs, it opens a database, inserts items into the table and then closes when the android application closes. I am looking for a way to update my code so that the table data can be saved permenantly on the device (ie data is saved somewhere when the application closes). This way the onCreate() method can just load the data instead of use open database, use insertTitle() method every time to populate the table. I am not using a specific device. I am using an emulator to run my application right now.

I am thinking of using SQLLite.create() method to store data permenantly, but I am not sure how to update my code. Any Ideas? I would like a specific answer with code if possible please.
Winner134
Freshman
Freshman
 
Posts: 4
Joined: Sun Aug 23, 2009 10:32 pm
Location: Canada

Top

Postby padde » Mon Aug 24, 2009 1:36 am

Well.. i think your problem is the "Wipe all data" setting... even on the emulator the SQLite DB is permanent...
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby Winner134 » Mon Aug 24, 2009 6:01 am

What happens if I add this line somewhere in DBAdapter class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. SQLiteDatabase.openDatabase("/nutrition.db3", null, SQLiteDatabase.OPEN_READWRITE + SQLiteDatabase.CREATE_IF_NECESSARY);
  5.  
  6.  
  7.  
  8.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Does this do anything different from the code I already have?
Winner134
Freshman
Freshman
 
Posts: 4
Joined: Sun Aug 23, 2009 10:32 pm
Location: Canada

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 7 guests