我正在尝试使用SQLiteOpenHelper将多个表存储到数据库中。我想出了这个共同的基类。
public abstract class BaseHelper<T> extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 5;
// Database Name
private static final String DATABASE_NAME = "helperdb";
public BaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// TODO: Add Java docs here
public abstract String getTableName();
public abstract String getCreateQuery();
public abstract ContentValues getValues(T item);
public abstract T get(Object... id);
public abstract T create(Cursor cursor);
public abstract int update(T item);
public abstract void delete(T item);
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(getCreateQuery());
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(PlayerHelper.class.getName(), "Upgrading database from version "
+ oldVersion + " to " + newVersion
+ ", which will destroy all old data");
db.execSQL(getUpdateQuery());
onCreate(db);
}
public String getUpdateQuery() {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder();
sb.append("DROP TABLE IF EXISTS ");
sb.append(getTableName());
return sb.toString();
}
public List<T> getAll() {
List<T> itemList = new ArrayList<T>();
// Select All Query
String selectQuery = "SELECT * FROM " + getTableName();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
T item = create(cursor);
itemList.add(item);
} while (cursor.moveToNext());
}
return itemList;
}
// Getting contacts Count
public int getCount() {
String countQuery = "SELECT * FROM " + getTableName();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
public void add(T item) {
SQLiteDatabase db = this.getWritableDatabase();
// Inserting Row
db.insert(getTableName(), null, getValues(item));
db.close(); // Closing database connection
}
}
然后我在这里有多个实现是1的例子。
public class TeamHelper extends BaseHelper<Team> {
private static final String TABLE_NAME = "teams";
public TeamHelper(Context context) {
super(context);
}
@Override
public String getCreateQuery() {
StringBuilder creator = new StringBuilder();
creator.append("CREATE TABLE ").append(getTableName()).append("(");
for (Team.SQLValues v : Team.SQLValues.values()) {
creator.append(v.toString()).append(" ")
.append(Team.SQLValues.getType(v)).append(", ");
}
return creator.substring(0, creator.length() - 2) + ")";
}
@Override
public String getTableName() {
// TODO Auto-generated method stub
return TABLE_NAME;
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
@Override
public ContentValues getValues(Team item) {
ContentValues values = new ContentValues();
for (Team.SQLValues v : Team.SQLValues.values()) {
switch (v) {
case TEAM_ID:
values.put(v.toString(), Long.toString(item.getTeamId()));
break;
case TEAM_NAME:
values.put(v.toString(), item.getTeamName());
break;
case RANK:
values.put(v.toString(), item.getRank());
break;
default:
break;
}
}
return values;
}
@Override
// Getting single contact
public Team get(Object... id) {
if(id.length != 1){
return null;
}
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME,
new String[] { Team.SQLValues.TEAM_ID.toString(),
Team.SQLValues.TEAM_NAME.toString(),
Team.SQLValues.TOTALPOINTS.toString() },
Team.SQLValues.TEAM_ID.toString() + "=?",
new String[] { String.valueOf(id[0]) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
return create(cursor);
}
public Team create(Cursor cursor) {
Team contact = new Team();
contact.setTeamId(Long.parseLong(cursor.getString(0)));
contact.setTeamName(cursor.getString(1));
contact.setTotalPoints(Integer.parseInt(cursor.getString(2)));
return contact;
}
// Updating single contact
@Override
public int update(Team contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Team.SQLValues.TEAM_NAME.toString(), contact.getTeamName());
// values.put(KEY_PH_NO, contact.getPhoneNumber());
// updating row
return db.update(TABLE_NAME, values, Team.SQLValues.TEAM_ID.toString()
+ " = ?", new String[] { String.valueOf(contact.getTeamId()) });
}
// Deleting single contact
@Override
public void delete(Team contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, Team.SQLValues.TEAM_ID.toString() + " = ?",
new String[] { String.valueOf(contact.getTeamId()) });
db.close();
}
}
然后我在活动中单独调用这些SQLLiteHelper ......
public static List<Team> getTeams(Context c) {
TeamHelper db = new TeamHelper(c);
return db.getAll();
}
public static List<Player> getPlayers(Context x){
PlayerHelper h = new PlayerHelper(x);
return h.getAll();
}
public static List<PlayerTeam> getPlayerTeams(Context x){
PlayerTeamHelper h = new PlayerTeamHelper(x);
return h.getAll();
}
List<Team> t = LoginTask.getTeams(this);
List<Player> t2 = LoginTask.getPlayers(this);
List<PlayerTeam> t3 = LoginTask.getPlayerTeams(this);
这里的问题是第一个工作正常,第二个和第三个说没有匹配的表。如果我改变顺序,那么有效的表也会改变。谁能看到我做错了什么?你能不能拥有一个以上的助手吗?
答案 0 :(得分:1)
你似乎过于复杂。每个数据库只需要一个帮助器而不是每个表。
使用多个帮助程序可能会让您遇到各种麻烦,例如数据库锁定异常。这也使得执行多表交易变得非常不可能。
看看我之前发布的这个答案,以获得一种简单易用的数据访问方式。