こんにちは、私は Android 開発の初心者です。私が抱えている問題は、テーブルを正常に作成してテーブルにデータを追加できることですが、データベースからデータを取得できず、arraylist は常に null 値を返します。私を助けてください :-)
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;
}
}