English 中文(简体)
无法从 Android SQLite 数据库获取数据
原标题:Cannot retrieve data from Android SQLite database

嗨,我是安卓开发的新人。我面临的问题是,我可以成功创建一个表格,并将数据添加到表格中,然而我无法从数据库中检索数据,而阵列列表总是返回无效值。请帮助我:-)

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


   }
最佳回答

我首先注意到的是,你正在使用 getString 尝试从光标上获取整数。 您应该使用 get Int

其次,您没有“_id”列作为主索引,所以您返回的光标不会与任何标准适配器一起工作。他们期望并要求在光标中有一个“_id”列。

问题回答

您确定要在表格中插入值吗?

我认为您应该使用 SQLiteDatabase db = this.getWritiableDatabase (); 而不是 SQLiteDatabase db = this.getReadableDatabase ();在添加 Blood Pressure 函数中的

试试这个,我帮你改

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




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签