嗨,我是安卓开发的新人。我面临的问题是,我可以成功创建一个表格,并将数据添加到表格中,然而我无法从数据库中检索数据,而阵列列表总是返回无效值。请帮助我:-)
public class DatabaseHandler extends SQLiteOpenHelper
{
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "BloodPressureRecorder";
// Bloodpressure table name
private static final String TABLE_BLOOD_PRESSURE = "BloodPressure";
// Bloodpressure Table Columns names
private static final String KEY_SYS = "sys";
private static final String KEY_DIA = "dia";
private static final String KEY_RATE = "rate";
SQLiteDatabase db;
public DatabaseHandler(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating tables
@Override
public void onCreate(SQLiteDatabase db)
{
String CREATE_BLOODPRESSURE_TABLE = "CREATE TABLE " + TABLE_BLOOD_PRESSURE +
"(" + KEY_SYS + " INTEGER," + KEY_DIA + " INTEGER, "
+ KEY_RATE + " INTEGER" + ")";
db.execSQL(CREATE_BLOODPRESSURE_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_BLOOD_PRESSURE);
// Create tables again
onCreate(db);
}
// Adding new blood pressure
public void addBloodPressure(BloodPressureResult bloodPressure)
{
SQLiteDatabase db = this.getReadableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_SYS, bloodPressure.getSys()); // Systolic
values.put(KEY_DIA, bloodPressure.getDia()); // Diastolic
values.put(KEY_RATE, bloodPressure.getRate()); // Rate
// Inserting Row
db.insert(TABLE_BLOOD_PRESSURE, null, values);
db.close(); // Closing database connection
}
// Getting all bloodpressures
public List<BloodPressureResult> getAllBloodPressure()
{
List<BloodPressureResult> bloodP = new ArrayList<BloodPressureResult>();
String selectQuery = "SELECT * FROM " + TABLE_BLOOD_PRESSURE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst())
{
do
{
BloodPressureResult bloodPressureResult = new BloodPressureResult();
bloodPressureResult.setSys(Integer.parseInt(cursor.getString(0)));
bloodPressureResult.setDia(Integer.parseInt(cursor.getString(1)));
bloodPressureResult.setRate(Integer.parseInt(cursor.getString(2)));
bloodP.add(bloodPressureResult);
}while(cursor.moveToNext());
}
cursor.close();
return bloodP;
}
}