我正在尝试了解如何处理SQLite。我有一个包含以下类的成员数据库:
package 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,例如,如果他们有更多的phonenumbers? (2)
并且,如果我删除数据库中的一行,是否更改了以下行的ID?或者,当我想要检索相同的成员时,我是否可以始终引用相同的ID?那么通过其他方式,一旦创建,成员保持相同的ID? (3)
答案 0 :(得分: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) });
}
至于保留多个电话号码,您可能最好创建第二个表。它可以包含_id
,phone
和phone_type
列。然后,您可以从主表中删除phone
作为列,或者将其用作“主要”电话号码,并使用电话表获取其他号码。
关于您的新问题3:删除行时,不会影响其他行(限制触发器)。特别是,没有其他ID值会改变。