无法从Android SQLite数据库中检索数据

时间:2012-05-28 05:19:02

标签: android sqlite

您好我是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;
   }


   }

3 个答案:

答案 0 :(得分:0)

我注意到的第一件事是您正在使用getString尝试从游标中检索整数。您应该使用getInt

其次,您没有作为主索引的“_id”列,因此您返回的光标将无法与任何标准适配器一起使用。他们期望并要求“_id”列在光标中。

答案 1 :(得分:0)

您确定要将值插入表中吗?

我认为你应该在addBloodPressure函数中使用SQLiteDatabase db = this.getWritableDatabase();而不是SQLiteDatabase db = this.getReadableDatabase();

答案 2 :(得分:0)

试试这个,我为你修改

public List<BloodPressureResult> getAllBloodPressure(){




List<BloodPressureResult> bloodP = new ArrayList<BloodPressureResult>();


final String[] columns = new String[] { KEY_SYS, KEY_DIA, KEY_RATE };
SQLiteDatabase db = getReadableDatabase();
final Cursor cursor = db.query(TABLE_BLOOD_PRESSURE, columns, "", null, "", "", "", "");
if(cursor.getCount>0){

while (cursor.moveToNext()) {
BloodPressureResult bloodPressureResult = new BloodPressureResult();
bloodPressureResult.setSys(cursor.getInt(cursor.getColumnIndex(KEY_SYS)));      
bloodPressureResult.setDia(cursor.getInt(cursor.getColumnIndex(KEY_DIA)));                 
bloodPressureResult.setRate(cursor.getInt(cursor.getColumnIndex(KEY_RATE)));     
bloodP.add(bloodPressureResult);   
}
}
cursor.close();
db.close();}