views:

55

answers:

1

I have a database in my application that is used as a ContentProvider.

It holds settings values for the application, and when I install the application I want it to add a hardcoded set of values just once.

This is how I am trying to do it at the minute.

 if(settings.size()<= 0){
   Settings s = new Settings("voipusernameTa", "xxxxxxxxx", "xxxxx",
   "displayNameTa", "sip.networks.com", "sip.networks.com", "siprealmTa", 120);
  addNewSettings(s);
 }

And this is the addNewSettings method:

private void addNewSettings(Settings _settings) {
   ContentResolver cr = getContentResolver();

   String w = CiceroSettings._ID + " = " + _settings.get_Id();
   Log.d("ADDNEW SETTINGS", "new setting id =" + _settings.get_Id());

   Cursor c = cr.query(CiceroSettings.CONTENT_URI, null, w, null, null);
   Log.d("CURSOR", "cursor created");

   int dbCount = c.getCount();
   c.close();

   if (dbCount == 0){
     ContentValues values = new ContentValues();

     values.put(Settings._ID, _settings.get_Id());
     values.put(Settings.VOIPUSERNAME, _settings.getVoipUserName());
     values.put(Settings.VOIPAUTHID, _settings.getVoipAuthId());
     values.put(Settings.PASSWORD, _settings.getPassword());
     values.put(Settings.VOIPDISPLAYNAME, _settings.getVoipDisplayName());
     values.put(Settings.SIPPROXYSERVER, _settings.getSipProxyServer());
     values.put(Settings.SIPREGISTRAR, _settings.getSipRegistrar());
     values.put(Settings.SIPREALM, _settings.getSipRealm());
     values.put(Settings.EXPIRESTIME, Integer.toString(_settings.getExpiresTime()));
     Log.d("CURSOR", "Values assigned");
     cr.insert(CiceroSettings.CONTENT_URI, values);
     Log.d("CURSOR", "Values inserted");
   }
 }

}

This works however each time the app starts it adds a new settings object and I only want it to app ONE object at the install of the application and thats it no more.

Has anyone a better solution for this? Or a reason why my code is flawed?

+1  A: 

If you want to add default values to your SQL table, I think the best way would be to set them right after creating your table.

So if you are using SQLiteOpenHelper, in the onCreate method, you can call db.insert() right after creating your table so that those values will be created just once

ccheneson
is there any reason why this would work on 1.5 but not on 1.6?
Donal Rafferty
Hum - that is weird . Since you use db.insert(), can you insert any data to db from your app? You can also try inserting your default values with using db.execSQL and an INSERT query
ccheneson