0

我试图了解如何处理 SQLite。我有一个具有以下类的成员数据库:

包 com.appiclife.saldo.data;

public class Member {

    private long id;
    private String name;
    private String phone;
    private String email;
    private int weight;

     public long getId() {
            return id;
          }

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

      public String getName() {
        return name;
      }

      public void setName(String name) {
        this.name = name;
      }

      public String getPhone() {
            return phone;
          }

      public void setPhone(String phone) {
            this.phone = phone;
          }

      public String getEmail() {
                    return email;
                  }

      public void setEmail(String email) {
                    this.email = email;
                  }   
      public int getWeight() {
                    return weight;
                          }

      public void setWeight(int weight) {
                    this.weight = weight;
                          }

然后我有以下 HelperClass 来创建和更新数据库:

public class MemberSQLiteHelper extends SQLiteOpenHelper {

  public static final String TABLE_MEMBERS = "members";
  public static final String COLUMN_ID = "_id";
  public static final String COLUMN_NAME = "name";
  public static final String COLUMN_PHONE = "phone";
  public static final String COLUMN_EMAIL = "email";
  public static final String COLUMN_WEIGHT = "weight";

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

  // Database creation sql statement
  private static final String DATABASE_CREATE = 
          "create table "
        + TABLE_MEMBERS + "(" + 
          COLUMN_ID + " integer primary key autoincrement, " + 
          COLUMN_NAME + " text not null, "+
          COLUMN_PHONE + " text, "+
          COLUMN_EMAIL + " text, "+
          COLUMN_WEIGHT + " integer);";

  public MemberSQLiteHelper(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(MemberSQLiteHelper.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBERS);
    onCreate(db);
  }

} 

我有以下方法:

public class MembersDataSource {

  // Database fields
  private SQLiteDatabase database;
  private MemberSQLiteHelper dbHelper;
  private String[] allColumns = { MemberSQLiteHelper.COLUMN_ID,
      MemberSQLiteHelper.COLUMN_NAME, MemberSQLiteHelper.COLUMN_PHONE, MemberSQLiteHelper.COLUMN_EMAIL, MemberSQLiteHelper.COLUMN_WEIGHT};

  public MembersDataSource(Context context) {
    dbHelper = new MemberSQLiteHelper(context);
  }

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

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

  public Member createMember(String name, String phone, String email, int weight) {
    ContentValues values = new ContentValues();
    values.put(MemberSQLiteHelper.COLUMN_NAME, name);
    values.put(MemberSQLiteHelper.COLUMN_PHONE, phone);
    values.put(MemberSQLiteHelper.COLUMN_EMAIL, email);
    values.put(MemberSQLiteHelper.COLUMN_WEIGHT, weight);
    long insertId = database.insert(MemberSQLiteHelper.TABLE_MEMBERS, null, values);
    Cursor cursor = database.query(MemberSQLiteHelper.TABLE_MEMBERS, allColumns, MemberSQLiteHelper.COLUMN_ID + " = " + insertId, null,
        null, null, null);
    cursor.moveToFirst();
    Member newMember = cursorToMember(cursor);
    cursor.close();
    return newMember;
  }

  public void deleteMember(Member member) {
    long id = member.getId();
    database.delete(MemberSQLiteHelper.TABLE_MEMBERS, MemberSQLiteHelper.COLUMN_ID
        + " = " + id, null);
  }

  public List<Member> getAllMembers() {
    List<Member> members = new ArrayList<Member>();

    Cursor cursor = database.query(MemberSQLiteHelper.TABLE_MEMBERS,
        allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Member member = cursorToMember(cursor);
      members.add(member);
      cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return members;
  }

  private Member cursorToMember(Cursor cursor) {
    Member member = new Member();
    member.setId(cursor.getLong(0));
    member.setName(cursor.getString(1));
    member.setPhone(cursor.getString(2));
    member.setEmail(cursor.getString(3));
    member.setWeight(cursor.getInt(4));
    return member;
  }
} 

还有问题,

例如,如何更改已经在数据库中的成员的名称?(1)

是否可以保存一个 Arraylist,例如,如果他们有更多的电话号码?(2)

而且,如果我删除数据库中的一行,ID 是否会更改以下行?或者当我想检索同一个成员时,我是否可以始终引用同一个 ID?那么通过其他方式,一旦创建,成员是否保持相同的 ID?(3)

4

1 回答 1

2

您只需要添加一个方法MembersDataSource来更新成员:

public void updateMember(Member member) {
    long id = member.getId();
    ContentValues values = new ContentValues();
    values.put(MemberSQLiteHelper.COLUMN_NAME, name);
    values.put(MemberSQLiteHelper.COLUMN_PHONE, phone);
    values.put(MemberSQLiteHelper.COLUMN_EMAIL, email);
    values.put(MemberSQLiteHelper.COLUMN_WEIGHT, weight);
    database.update(MemberSQLiteHelper.TABLE_MEMBERS, values,
        MemberSQLiteHelper.COLUMN_ID + " = ?",
        new String[] { Integer.toString(id) });
}

至于保留多个电话号码,您最好创建第二个表。它可以有_idphone和列phone_type。然后,您可以phone从主表中删除一列,或者将其用作“主要”电话号码,并将电话表用于其他号码。

关于您的新问题 3:当您删除一行时,不会影响其他行(禁止触发器)。特别是,其他 ID 值不会更改。

于 2013-01-08T00:24:40.727 回答