android sqlite从数据库中检索一个值?

时间:2016-08-27 16:45:04

标签: android sqlite

所以我有一个数据库,其中一个表有一列(COLUMN_ID,COLUMN_TARGET,COLUMN_CURRENT)。我正在尝试检索COLUMN_ID = 1的列COLUMN_TARGET的值,并将其保存在变量tar中并返回它但我的应用程序在运行时仍然崩溃。我使用log messeges来解决它出错的地方并打印出来#34; 1 ..."然后崩溃。

public int getTarget() {

    SQLiteDatabase db = this.getReadableDatabase();
    String[] columns = {COLUMN_TARGET};
    int tar=0;
    Log.d("tag","1...\n");
    Cursor c = db.query(TABLE_VALUES, columns, COLUMN_ID+"=?", new String[] { String.valueOf(1) }, null, null, null,null );
    Log.d("tag","2...\n");
    if (c.getCount() == 1) {
        Log.d("tag","3...\n");
        c.moveToFirst();
    }
    Log.d("tag","4...\n");
        tar = c.getInt(0);
    Log.d("tag","5...\n");
    db.close();
    c.close();
    Log.d("tag","6...\n");
   // return Integer.parseInt(tar);
    return tar;

}

有人可以帮我解决这个问题吗?!提前谢谢

1 个答案:

答案 0 :(得分:0)

尝试以下:

    String SELECT_SQL = "SELECT * FROM "+ TABLE_VALUES + "WHERE COLUMN_ID =" + 1 + ";";
    Cursor c = db.rawQuery(SELECT_SQL, null);
    if (c.getCount() == 1) {
       Log.d("tag","3...\n");
       c.moveToFirst();
       int tar = c.getInt(0);
    }