DB problem? Force close of application but not in emulator

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

DB problem? Force close of application but not in emulator

Postby E-Nature » Wed Feb 17, 2010 11:24 pm

I use a local sqlite database in my application. for a new version I added a new table to the database. when I test it with the emulator everything works just fine. but when I test it on my phone I get a force close whenever something is inserted or selected from the newly added table. why is that? what could be the problem?

do I have to use the onUpgrade() method when I add a new table to the database?

thanks for help. it's really dreadful :(
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Top

Postby mitutitu » Thu Feb 18, 2010 6:28 am

Hi E-Nature,

Would you like to paste error log from your LogCat. It'd be specific to find out problem exactly.

And of course when a schematic changes happen onUpgrade() would be called.
This method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version.

Thanks,
mitu
Android is awesome!
User avatar
mitutitu
Senior Developer
Senior Developer
 
Posts: 113
Joined: Tue Jun 09, 2009 7:40 am
Location: St. John's, NL, Canada

Postby E-Nature » Thu Feb 18, 2010 6:32 am

hi mitu
thanks for your help so far. I never tested it with the "onUpgrade" method. so I just have to increase the database version from 1 to 2 and that's it?

sorry for my dumb question but how can I get the "LogCat" file? you mean the LogCat file from my cell phone? (where the application crashes)
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Postby mitutitu » Thu Feb 18, 2010 6:51 am

Hi E-Nature,

Ya you are right. So whenever you need to change your DB schema as drop table, alter table, add table etc. All you have to do just upgrade your DB version and it will call onUpgrade() method. Inside this method you can do any sql execution for your purpose.

If you're developing your app on Eclipse IDE. you will get DDMS-->logCat. Here in logCat basically you can get all the happenings of your app including OS itself.

You also can get it From Eclipse menu: Window --> Open Perspective --> DDMS --> logCat, Console etc.

Thanks,
mitu
Android is awesome!
User avatar
mitutitu
Senior Developer
Senior Developer
 
Posts: 113
Joined: Tue Jun 09, 2009 7:40 am
Location: St. John's, NL, Canada

Postby E-Nature » Thu Feb 18, 2010 6:38 pm

thanks again :)

OK I understand. To my case with the databaase:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private static class DatabaseHelper extends SQLiteOpenHelper {
  2.  
  3.  
  4.  
  5.                 DatabaseHelper(Context context) {
  6.  
  7.                         super(context, DATABASE_NAME, null, DATABASE_VERSION);
  8.  
  9.                 }
  10.  
  11.  
  12.  
  13.                 public void onCreate(SQLiteDatabase db) {
  14.  
  15.                         db.execSQL(DATABASE_CREATE_MEMBER);
  16.  
  17.                         db.execSQL(DATABASE_CREATE_FAVORITES);
  18.  
  19.                 }
  20.  
  21.  
  22.  
  23.                 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  24.  
  25.                         Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
  26.  
  27.                         db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_MEMBERS);
  28.  
  29.                         db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_FAVORITES);
  30.  
  31.                         onCreate(db);
  32.  
  33.                 }
  34.  
  35.         }
Parsed in 0.013 seconds, using GeSHi 1.0.8.4


In my case I added a new table called DATABASE_TABLE_FAVORITES. The other table DATABASE_TABLE_MEMBERS remains unmodified. Do I have to drop both tables in OnUpgrade() ? or how would my code snippet look like in my case?
so in my logic, if I just add the new FAVORITES table I actually don't have to put anything in the OnUpgrade() method - or am I wrong?

thanks for help :-)
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Postby E-Nature » Thu Feb 18, 2010 9:39 pm

me again!
I solved the problem!
the fault I made was to call onCreate() IN the onUpgrade() method. the app wanted to create the tables twice and led to a force close. but now that I removed it the problem is gone :lol:
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 1 guest