Android SQLite查询不返回列值

时间:2014-08-05 08:09:07

标签: android sql sqlite persistent-storage

我无法从以下SQLite选择查询功能添加读取数据。我有一个包含3列的表,即driverID,driversName,driversNo。

它应该返回一个简单的字符串但是唉。

public String readDrivers() {
Cursor cursor = database.query(MySQLiteHelper.PROFILE_TABLE_NAME, necessaryColumns, null, null, null, null, null);

if (cursor != null){
    cursor.moveToFirst();
    Log.v("TEST", Integer.toString(cursor.getCount()));
    Log.v("TEST", cursor.getColumnName(0));
    Log.v("TEST", cursor.getColumnName(1));
    Log.v("TEST", cursor.getString(0));
    Log.v("TEST", cursor.getString(1));
}

String returnString = cursor.getString(0) + cursor.getString(1);
return returnString;
}

以下是LogCat的最新结果。两个字符串结果不显示。

08-04 20:27:35.271: V/TEST(25126): 1
08-04 20:27:35.271: V/TEST(25126): driversName
08-04 20:27:35.271: V/TEST(25126): driversNo

2 个答案:

答案 0 :(得分:1)

的问题
cursor.getString(0) 

我认为它是int值所以请尝试cursor.getInt(0)

有一些方法可以从Cursour获取数据

       public String readDrivers() {
    Cursor cursor = database.query(MySQLiteHelper.PROFILE_TABLE_NAME, necessaryColumns, null, null, null, null, null);

    if (cursor != null){
        cursor.moveToFirst();
        Log.v("TEST", Integer.toString(cursor.getCount()));
        Log.v("TEST", cursor.getColumnName(0));
        Log.v("TEST", cursor.getColumnName(1));
        Log.v("TEST", cursor.getString(0));
        Log.v("TEST", cursor.getString(1));
    }

    String returnString = cursor.getString(0) + cursor.getString(1);
    //you suppose to get data from cursor like
   String temp = cursour.getString(cursour.getColumIndex("YOUR_COLUMN_NAME"));
   Log.i(TAG , "Required Data:: "+temp);
    return returnString;
}

答案 1 :(得分:0)

尝试使用 isNull 方法检查列是否为空

由于您获得cursor.getColumnName(0));的输出,这意味着列索引0存在。因此,cursor.getString(0)不应被视为错误!

参考cursor.getString的文档:

它说:

The result and whether this method throws an exception when the 
column value is null or the column type is not a string type 
is implementation-defined.