如何从Android中的sqlite中的select语句返回一个字符串?

时间:2016-02-19 00:03:18

标签: android sqlite

String selectQuery = "SELECT accno from AccountMaster where pan = " + pan;

Cursor cursor = db.rawQuery(selectQuery, null);

int accno = 0;
if(cursor.moveToFirst())
accno=cursor.getInt(0);

1 个答案:

答案 0 :(得分:1)

首先,您必须确保要获取的列存储文本。在那种情况下使用

String selectQuery = "SELECT accno from AccountMaster where pan = " + pan;
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
   String value = cursor.getString(indexNumber)
}

否则,如果列存储了int,则可以执行此操作。

String selectQuery = "SELECT accno from AccountMaster where pan = " + pan;
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
   int value = cursor.getInt(indexNumber)
}

您也可以使用

将返回的int值转换为String值
String mValue = String.valueOf(intValue);