user1858863
user1858863

Reputation: 53

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

Upvotes: 3

Views: 222

Answers (3)

Harshid Vasoya
Harshid Vasoya

Reputation: 5721

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.

Upvotes: 0

Opiatefuchs
Opiatefuchs

Reputation: 9870

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
        + ");";

Upvotes: 1

Priety
Priety

Reputation: 308

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.

Upvotes: 0

Related Questions