2012-05-28 155 views
0

嗨,我是Android开发新手。我遇到的问题是我可以成功创建表并向表中添加数据,但是我无法从数据库中检索数据,并且arraylist始终返回空值。请帮我:-)无法从Android SQLite数据库检索数据

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; 
    } 


    } 
+0

发表您的logcat .. –

+0

只好尝试登录,看看您是否可以从游标的任何数据? – Hanon

+0

当你从db请求时检查你的游标值,它是NULL吗? –

回答

0

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

其次,您没有“_id”列是主索引,因此您返回的游标不适用于任何标准适配器。他们期望并且需要一个“_id”列在光标中。

+0

感谢您的回答,巴拉克。现在我可以从我的数据库中检索数据。 :-) – BenjaminChu

0

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

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

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();}