Search code examples
androidandroid-sqlitesqliteopenhelper

No database created after done changes in SQLite Database


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);
  }

Solution

  • 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.