3

can someone help in SQLite database in android?

First all,i have using tutorial from here. It works well in my application but when i added new column and change database version to '2', the application stopped and in the file explorer, no database found.could anyone state whats the problem is and the solution.

edited: here is some my changes.. this is my FirstClass.java

// Database creation SQL statement
  private static final String DATABASE_CREATE = "create table " 
      + TABLE_TODO
      + "(" 
      + COLUMN_ID + " integer primary key autoincrement, " 
      + COLUMN_CATEGORY + " text not null, " 
      + COLUMN_SUMMARY + " text not null," 
      + COLUMN_DESCRIPTION
      + " text not null" 
      + COLUMN_DATE + "date not null " //i have added the date column
      + ");";
     public static void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);

  }

  public static void onUpgrade(SQLiteDatabase database, int oldVersion,
      int newVersion) {
    Log.w(FirstClass.class.getName(), "Upgrading database from version "
        + oldVersion + " to " + newVersion
        + ", which will destroy all old data");
    database.execSQL("DROP TABLE IF EXISTS " + TABLE_TODO);
   // database.execSQL("INSERT INTO "+TABLE_TODO+" VALUES (null, datetime()) ");
    onCreate(database);
  }

and here is my TodoDatabaseHelper.java

private static final String DATABASE_NAME = "todotable2.db";
  private static final int DATABASE_VERSION = 2;

  public TodoDatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  // Method is called during creation of the database
  @Override
  public void onCreate(SQLiteDatabase database) {
    FirstClass.onCreate(database);
  }

  // Method is called during an upgrade of the database,
  // e.g. if you increase the database version
  @Override
  public void onUpgrade(SQLiteDatabase database, int oldVersion,
      int newVersion) {
    FirstClass.onUpgrade(database, oldVersion, newVersion);
  }

3 Answers3

1

I am not sure if I am right, but, if You delete Your db in Your oUnUpgrade-method, which dab should the system upgrade? I mean, If You set a new version for Your db, it should be the same db as before, but only changed and not dropped.

I had an similar case, I saved some user input value, but every time a crash came over my app. So I set the versions to the same integer (for old db and new db only version 1 for example, not version 1 for old and version 2 for new), deleted the old db and created a completely new one with all new datas inside. This worked very good....

EDIT

The statement above was for update, not for upgrade, sorry. But now, that I have looked at your code, is it possible that no db is created because of an error? I think You forgot to set a point after COLUMN_DESCRIPTION. Here is my edit:

      private static final String DATABASE_CREATE = "create table " 
        + TABLE_TODO
        + "(" 
        + COLUMN_ID + " integer primary key autoincrement," 
        + COLUMN_CATEGORY + " text not null," 
        + COLUMN_SUMMARY + " text not null," 
        + COLUMN_DESCRIPTION
        + " text not null," //HERE YOU FORGOT COMMA
        + COLUMN_DATE + " date not null" //i have added the date column
        + ");";
Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • how you delete your old db and create new one? is changing the db name will create new db? – user1858863 Nov 28 '12 at 08:39
  • 1
    For now, I have no laptop by my side, so I can´t look at my code. I think Your onUpgrade method will work, if You set in your ToDoDatabaseHelper Class in onUpgrade for oldVersion and newVersion the same integer for example, set an final integer oldVersion = 1, newVersion = 1. I will give You an exact example when I am at home this evening. – Opiatefuchs Nov 28 '12 at 09:07
  • 1
    Hi, I´m back at home..so first I got a Question, just to understand You the right way. Do You really want to insert a new column (for example as a new feature for the user), or do You want to insert a new row (like an user input)? – Opiatefuchs Nov 28 '12 at 17:36
  • sorry im a bit late noticing your comment..btw,thanx for your time..for now,i want to insert new column...after solving this problem, i will insert a new row... – user1858863 Nov 29 '12 at 02:05
  • No problem, I slept in the meantime(in Germany it´s night). Ok, so I think I misinterpreted Your needs. But I looked at Your code, and maybe there is a little error inside. Look at my Edit.... – Opiatefuchs Nov 29 '12 at 05:16
  • thanks.. it help. now my database can work well. thanks again. – user1858863 Nov 30 '12 at 01:34
0

According to me the method

@Override public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { FirstClass.onUpgrade(database, oldVersion, newVersion); }

is imbibing the problem. Because you are just upgrading the version but not creating the database again.After you upgrade the database you need to call the onCreate just as you have done for the tables.

Priety
  • 308
  • 1
  • 4
  • 19
  • you are right about upgrading the version, i just want to know if it might be the problem... i think my problem occurs after i added new column in the table because i've read in android developer about altering the table might cause the table to drop.. – user1858863 Nov 28 '12 at 08:43
  • Yeah. Thats true. If you modify the table the table will be dropped.Better is to write a Query for any modifications to the table and to call the method whenever you want to modify the table. – Priety Nov 28 '12 at 08:45
  • thanx... i've thought about that after i 'corrupt' the table... i will do that in future.. – user1858863 Nov 28 '12 at 08:49
  • 1
    Changing the db will create a new DB. Or even you can Query it to drop the DB. – Priety Nov 28 '12 at 08:49
0

using onUpgrade() you just remove table structure not entire database.

Solution:

(1) Remove your application pro grammatically when you update database.

(2) you have to uninstall your application in your mobile and reinstall.

Harshid
  • 5,701
  • 4
  • 37
  • 50