在android中的SQLite中添加一个新字段

时间:2013-07-27 12:47:41

标签: java android sqlite

正如我之前所说的那样,我非常喜欢android,现在我遇到了一个非常令人不安的问题。 我有一个数据库,我需要在表中添加一个新字段。我不太了解android和java .. 所以请大家,一点帮助将不胜感激...... 这是我的数据库代码:

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

当我只使用一个字段(即评论)时,所有这些代码都可以正常工作 我想添加三个名为“Address”,“Age”,“Gender”的新字段,如果可能的话,更改数据库名称,我似乎无法以正确的方式改变它以获得我想要的东西。 请告诉我所需的正确改动。 请帮助我!

提前致谢, 等待你的回复...

2 个答案:

答案 0 :(得分:1)

  

如果可能的话更改数据库名称,我似乎无法做到   以正确的方式改变它以获得我想要的东西。请告诉我正确的   需要改变。

有两种方法:

  • 正确实施onUpgrade()类的SQLiteOpenHelper方法(如果您需要更新)

  • 只需更新您的数据库类(修改DDL语句e.q.添加新列并更改数据库 如果您愿意,可以在将更新后的应用程序安装到您的设备之前删除 应用程序的数据(在设备中,它位于应用程序的设置中) 经理)。这将删除连接到您的应用程序的所有数据 (数据库,偏好等)

看看

答案 1 :(得分: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 );

(这里,列名是静态字符串,声明为类变量)。

相关问题