2013-08-01 63 views
3

如何在現有表LOGIN中添加列。這裏是我的示例代碼。如何在Android SQLite的現有表上添加列?

這是我DataBaseAdapter類:

public class DataBaseHelper extends SQLiteOpenHelper 
{ 
    public DataBaseHelper(Context context, String name,CursorFactory factory, int version) 
    { 
       super(context, name, factory, version); 
    } 
    // Called when no database exists in disk and the helper class needs 
    // to create a new one. 
    @Override 
    public void onCreate(SQLiteDatabase _db) 
    { 
      _db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE); 

    } 
    // Called when there is a database version mismatch meaning that the version 
    // of the database on disk needs to be upgraded to the current version. 
    @Override 
    public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) 
    { 
      // Log the version upgrade. 
      Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data"); 


      // Upgrade the existing database to conform to the new version. Multiple 
      // previous versions can be handled by comparing _oldVersion and _newVersion 
      // values. 
      // The simplest case is to drop the old table and create a new one. 
      _db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE"); 
      // Create a new one. 
      onCreate(_db); 
    } 

這是我LoginDataBaseAdapter

public class LoginDataBaseAdapter 
{ 
     static final String DATABASE_NAME = "login.db"; 
     static final int DATABASE_VERSION = 1; 
     public static final int NAME_COLUMN = 1; 
     // TODO: Create public field for each column in your table. 
     // SQL Statement to create a new database. 
     static final String DATABASE_CREATE = "create table "+"LOGIN"+ 
            "(" +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text); "; 
     // Variable to hold the database instance 
     public SQLiteDatabase db; 
     // Context of the application using the database. 
     private final Context context; 
     // Database open/upgrade helper 
     private DataBaseHelper dbHelper; 
     public LoginDataBaseAdapter(Context _context) 
     { 
      context = _context; 
      dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 
     public LoginDataBaseAdapter open() throws SQLException 
     { 
      db = dbHelper.getWritableDatabase(); 
      return this; 
     } 
     public void close() 
     { 
      db.close(); 
     } 

     public SQLiteDatabase getDatabaseInstance() 
     { 
      return db; 
     } 

     public void insertEntry(String userName,String password) 
     { 
      ContentValues newValues = new ContentValues(); 
      // Assign values for each row. 
      newValues.put("USERNAME", userName); 
      newValues.put("PASSWORD",password); 

      // Insert the row into your table 
      db.insert("LOGIN", null, newValues); 
      ///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show(); 
     } 
     public int deleteEntry(String UserName) 
     { 
      //String id=String.valueOf(ID); 
      String where="USERNAME=?"; 
      int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ; 
      // Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show(); 
      return numberOFEntriesDeleted; 
     } 
     public String getSinlgeEntry(String userName1) 
     { 
      Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName1}, null, null, null); 
      if(cursor.getCount()<1) // UserName Not Exist 
      { 
       cursor.close(); 
       return "NOT EXIST"; 
      } 
      cursor.moveToFirst(); 
      String password= cursor.getString(cursor.getColumnIndex("PASSWORD")); 
      cursor.close(); 
      return password;     
     } 
     public void updateEntry(String userName,String password) 
     { 
      // Define the updated row content. 
      ContentValues updatedValues = new ContentValues(); 
      // Assign values for each row. 
      updatedValues.put("USERNAME", userName); 
      updatedValues.put("PASSWORD",password); 

      String where="USERNAME = ?"; 
      db.update("LOGIN",updatedValues, where, new String[]{userName});    
     }  
} 

如何添加列FIRSTNAME(從TextView的),姓氏(從TextView的),部門(從微調)。

回答

0

你必須首先更新您的SQLite數據庫版本,那麼將運行onUpgrade()方法,將放棄所有您的數據。然後該表將重新映射您在DATABASE_CREATE字符串中定義的新模式。

所以這個自帶的主要問題是,你必須找到一種方法來恢復已經在表中已有的表將被刪除的數據。在這種情況發生之前,你確實可以運行onUpgrade,儘管這樣使用這個方法作爲一個點來保存你需要從數據庫中獲取的任何數據。

static final int DATABASE_VERSION = 2; 

並更新您的數據庫創建字符串。

+0

你的意思是說生病改變靜態最終詮釋DATABASE_VERSION = 1;到static final int DATABASE_VERSION = 2;然後我將添加的列等this..static最終字符串DATABASE_CREATE =「創建表‘+’LOGIN」 + 「(」 +「ID」 +「整數主鍵自動增量,」 +「USERNAME文本,密碼文本, FIRSTNAME文本,LASTNAME文本,DEPARTMENT文本);「;就像那樣? –

+0

是的,但只能這樣做,如果你沒有丟失表中的數據 –

+0

是的,但它不會更新。相反,我需要更改整個表格.- –

0

您還可以改變現有的表,如果你想要做的就是添加一個或多個列。所以說你要來更新數據庫版本號添加一個名爲「my_new_col」到一個名爲「MY_TABLE」表中的新列中,除了從1到2,你可以更新表的模式不會丟失任何數據

... 
@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    switch(newVersion) { 

     case 2: /* this is your new version number */ 

      // ... Add new column 'my_new_col' to table 'my_table' 
      db.execSQL("alter table my_table add column my_new_col") ; 
      break ; 
    } 
} 

就是這樣。當然,您想要在新列上定義附加約束,並且您希望確保新列在'onCreate(...)'中包含在原始表的創建中。