2013-07-27 28 views
-1

正如我之前所說的,我對android非常陌生,現在我面臨一個非常令人困擾的問題。 我有一個數據庫,我需要在表中添加一個新的字段。我不知道很多的Android和Java的.. 所以,請人,一點點幫助將不勝感激...... 這裏是我的數據庫代碼:在android中添加一個新字段在android中

public class MySQLiteHelper extends SQLiteOpenHelper { 

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

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

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

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

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

    @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); 
    onCreate(db); 
    } 

} 

這是數據庫的功能:

public class CommentsDataSource { 

    // Database fields 
    private SQLiteDatabase database; 
    private MySQLiteHelper dbHelper; 
    private String[] allColumns = { MySQLiteHelper.COLUMN_ID, 
     MySQLiteHelper.COLUMN_COMMENT }; 

    public CommentsDataSource(Context context) { 
    dbHelper = new MySQLiteHelper(context); 
    } 

    public void open() throws SQLException { 
    database = dbHelper.getWritableDatabase(); 
    } 

    public void close() { 
    dbHelper.close(); 
    } 

    public Comment createComment(String comment) { 
    ContentValues values = new ContentValues(); 
    values.put(MySQLiteHelper.COLUMN_COMMENT, comment); 
    long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null, 
     values); 
    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, 
     allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null, 
     null, null, null); 
    cursor.moveToFirst(); 
    Comment newComment = cursorToComment(cursor); 
    cursor.close(); 
    return newComment; 
    } 

    public void deleteComment(Comment comment) { 
    long id = comment.getId(); 
    System.out.println("Comment deleted with id: " + id); 
    database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID 
     + " = " + id, null); 
    } 

    public List<Comment> getAllComments() { 
    List<Comment> comments = new ArrayList<Comment>(); 

    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, 
     allColumns, null, null, null, null, null); 

    cursor.moveToFirst(); 
    while (!cursor.isAfterLast()) { 
     Comment comment = cursorToComment(cursor); 
     comments.add(comment); 
     cursor.moveToNext(); 
    } 
    // Make sure to close the cursor 
    cursor.close(); 
    return comments; 
    } 

    private Comment cursorToComment(Cursor cursor) { 
    Comment comment = new Comment(); 
    comment.setId(cursor.getLong(0)); 
    comment.setComment(cursor.getString(1)); 
    return comment; 
    } 
} 

,這是上面的類使用其他類:

public class Comment { 
    private long id; 
    private String comment; 

    public long getId() { 
    return id; 
    } 

    public void setId(long id) { 
    this.id = id; 
    } 

    public String getComment() { 
    return comment; 
    } 

    public void setComment(String comment) { 
    this.comment = comment; 
    } 

    // Will be used by the ArrayAdapter in the ListView 
    @Override 
    public String toString() { 
    return comment; 
    } 
} 

所有這些代碼工作得很好,當我只有一個字段(我的工作。即評論),但 我想添加三個新的字段名爲:「地址」,「年齡」,「性別」,如果可能的話改變DB名稱,我似乎無法以正確的方式改變它得到我想要的。 請告訴我需要的正確改動。 請幫我解決這個問題!

由於提前, 等待你的答覆...

+0

你真的需要更新現有的數據庫嗎?我假設你的應用程序尚未使用,所以你不能只創建一個適合你需要的新數據庫嗎?或者實際上你在問什麼? –

+0

我必須更新數據庫。 –

+0

:(沒有其他選擇 –

回答

1

如果可能的話改變數據庫的名字,我只是不能似乎能夠 改變它以正確的方式得到我想。請告訴我需要更改的權利 。

有兩種方式:

  • 實現正確onUpgrade()方法SQLiteOpenHelper類的(如果你需要更新)

  • 只需更新數據庫類(修改DDL聲明EQ添加新列和如果需要,更改db 的名稱),並在將更新的應用程序安裝到設備之前,刪除 應用程序的數據(在設備中,它位於應用程序的設置下 經理)。這將刪除

+0

請告訴我如何。PLEEEEEEASEEEEE –

+0

大約一週前我開始了android編程,所以,我不知道它的完整結構呢... :( –

+0

@ShanzidSHaiham我在我的起源答案中加入了鏈接 – Sajmon

1

連接到你的應用程序 所有數據(數據庫,喜好等),這樣在創建表中的新列:

String Comment_table = String.format("CREATE TABLE %s " + 
       "(%s INTEGER PRIMARY KEY AUTOINCREMENT ," + 
       " %s TEXT , %s TEXT , %s TEXT ," + 
       " %s TEXT )", 

       Comment_Table, 
       Comment_ID , 
       Comment , Address , Age , 
       Gender); 

(這裏列名是St atic字符串被聲明爲類變量)。

+0

我在哪裏可以把這個?哦哦 –

+0

提供SQLHelper類。 –

+0

,其中在輔助類? –

相關問題