我有一个 DatabaseHelper 类和 DatabaseAdapter 类。当我尝试从多个活动中选择数据时,我得到空数据库。我究竟做错了什么?我在调用数据库适配器并将当前活动作为上下文传递时正在读取或写入 dabase。
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "MySampleDatabase";
private static final int DATABASE_VERSION = 15;
private final Context context;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
AssetManager manager = context.getResources().getAssets();
InputStream is;
try {
is = manager.open("SQLStatements");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
String line;
while((line = reader.readLine()) != null){
sb.append(line);
}
String[] queries = (new String(sb)).split(";");
is.close();
for(int i = 0; i < queries.length; i++){
db.execSQL(queries[i]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS UserDrugs");
db.execSQL("DROP TABLE IF EXISTS UserDrugsUsages");
onCreate(db);
}
}
public class DatabaseUserDrugsUsagesAdapter {
public static final String KEY_USER_DRUG_ID = "UserDrugID";
public static final String KEY_PACKAGE_ID = "PackageID";
public static final String KEY_TIME_TO_USE = "TimeToUse";
public static final String KEY_USED = "Used";
public static final String KEY_PACKAGE_NAME = "PackageName";
private static final String DATABASE_TABLE = "UserDrugsUsages";
private Context context;
private SQLiteDatabase database;
private DatabaseHelper dbHelper;
public DatabaseUserDrugsUsagesAdapter(Context context) {
this.context = context;
}
public DatabaseUserDrugsUsagesAdapter open() throws SQLException {
dbHelper = new DatabaseHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}
public void clearDatabaseTable(){
database.execSQL("DELETE FROM " + DATABASE_TABLE);
}
public Cursor selectUserDrugUsagesByDate(String date){
return database.query(DATABASE_TABLE, new String[] {
KEY_USER_DRUG_ID,
KEY_PACKAGE_ID,
KEY_TIME_TO_USE,
KEY_USED,
KEY_PACKAGE_NAME}, "TimeToUse like '" + date + "%'", null, null, null, null);
}
public long writeUserDrugUsage(int userDrugId, int packageId, String timeToUse, boolean used, String packageName) {
ContentValues initialValues = createContentValues(userDrugId, packageId, timeToUse, used, packageName);
return database.insert(DATABASE_TABLE, null, initialValues);
}
private ContentValues createContentValues(int userDrugId, int packageId, String timeToUse, boolean used,
String packageName) {
ContentValues values = new ContentValues();
values.put(KEY_USER_DRUG_ID, userDrugId);
values.put(KEY_PACKAGE_ID, packageId);
values.put(KEY_TIME_TO_USE, timeToUse);
values.put(KEY_USED, used);
values.put(KEY_PACKAGE_NAME, packageName);
return values;
}
}
编辑:有我调用选择函数的代码。
Cursor c = null;
String name = null;
DatabaseUserDrugsUsagesAdapter db = new DatabaseUserDrugsUsagesAdapter(context);
db.open();
c = db.selectDrugNameById(id);
db.close();
if(c != null && c.getCount() > 0){
c.moveToFirst();
name = c.getString(4);
}
c.close();