2013-12-23 92 views
-1

我有我已經創建了一個表table_comments數據庫comments.db現在 我想在這個數據庫中創建第二個表(table_birthday)。我的問題是如何在現有數據庫中添加此表?我有以下代碼。任何幫助讚賞。如何添加第二個表中的SQLite數據庫

package com.example.proj1; 


import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class MySQLiteHelper extends SQLiteOpenHelper { 

    //table 1 
    public static final String TABLE_COMMENTS = "comments"; 
     public static final String COLUMN_ID = "_id"; 
     public static final String COLUMN_COMMENT = "comment"; 

     //table 2 
     public static final String TABLE_BIRTHDAY = "birthday"; 
     public static final String Birthday_ID = "_id"; 
     public static final String COLUMN_Birthday = "bday"; 

     //database 
     private static final String DATABASE_NAME = "commments.db"; 
     private static final int DATABASE_VERSION = 1; 

     // table 1 
     private static final String DATABASE_CREATE = "create table " 
      + TABLE_COMMENTS + "(" + COLUMN_ID 
      + " integer primary key autoincrement, " + COLUMN_COMMENT 
      + " text not null);"; 

     //table 2 
     private static final String DATABASE_CREAT = "create table " 
       + TABLE_BIRTHDAY + "(" + Birthday_ID 
       + " integer primary key autoincrement, " + COLUMN_Birthday 
       + " text not null);"; 


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


    @Override 
    public void onCreate(SQLiteDatabase database) { 
     database.execSQL(DATABASE_CREATE); 
     database.execSQL(DATABASE_CREAT); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     Log.w(MySQLiteHelper.class.getName(),"Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); 
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS); 
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_BIRTHDAY); 
      onCreate(db); 

    } 

} 
+1

這段代碼應該這樣做......問題是什麼? – Kristopher

+0

http://stackoverflow.com/questions/9804083/how-to-add-second-table-in-database-in-sqlite 這個問題已經解決只是看到鏈接 – Arpan24x7

回答

2

使用應用程序管理器清除應用程序數據,或者只是卸載並重新安裝應用程序。

測試環境已經與架構版本1數據庫,當你加入另一個表修改了模式,數據庫沒有升級,也創造了自從模式版本是相同的,該數據庫已經存在。

對於正式發佈的版本,你應該不是撞了DB_VERSION和執行onUpgrade()相應的遷移代碼。

相關問題