0

我正在尝试在我的数据库中创建一个方法。我想从我的数据库中返回一个具有特定列名的特定行中的字符串。我试过这个,但它不起作用。有人能帮助我吗??

public String getNameAtPosition(String row){
    String name = null;

    Cursor c = mDb.query(ValuteMetaData.VALUTE_TABLE, null, ValuteMetaData.ID + "=" + row, null, null, null, null);

    name = String.valueOf(c.getLong(ValuteMetaData.VALUTE_NAME_KEY));

    return name;
}


public Cursor fetchValuteListView(String data){ 
        String[] data_query={data};
        return mDb.query(ValuteMetaData.VALUTE_TABLE,null,ValuteMetaData.VALUTE_DATA_KEY+"=?",data_query,null,null,null);              
}


static class ValuteMetaData {  // i metadati della tabella, accessibili ovunque
        static final String VALUTE_TABLE = "Valute";
        static final String ID = "_id";
        static final String VALUTE_NAME_KEY = "name";
        static final String VALUTE_SIGLA_KEY = "sigla";
        static final String VALUTE_CAMBIO_KEY = "cambio";
        static final String VALUTE_DATA_KEY = " data";
}

private static final String VALUTE_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "  //codice sql di creazione della tabella
                + ValuteMetaData.VALUTE_TABLE + " ("
                + ValuteMetaData.ID+ " integer primary key autoincrement, "
                + ValuteMetaData.VALUTE_NAME_KEY + " text, "
                + ValuteMetaData.VALUTE_SIGLA_KEY + " text, "
                + ValuteMetaData.VALUTE_CAMBIO_KEY + " text, "
                + ValuteMetaData.VALUTE_DATA_KEY + " text);";
4

3 回答 3

1

更改您的方法和查询如下:

public String getNameAtPosition(String row){
    String name = null;

    Cursor c = mDb.query(ValuteMetaData.VALUTE_TABLE, null,
            ValuteMetaData.ID + " = ?" , new String[]{row}, null, null, null);

    if(c.moveToFirst())
        name = c.getString(c.getColumnIndex(ValuteMetaData.VALUTE_NAME_KEY));

    return name;
}
于 2012-04-04T12:19:26.690 回答
1

在使用光标之前,您应该将其指向行。在你的情况下,你应该写:

c.moveToFirst();
于 2012-04-04T12:20:15.693 回答
0

改变:

name = String.valueOf(c.getLong(ValuteMetaData.VALUTE_NAME_KEY));

to(注意“getString”和“getColumnIndex”)

if(c.moveToFirst()){
    name = String.valueOf(c.getString(c.getColumnIndex(ValuteMetaData.VALUTE_NAME_KEY)));
}
于 2012-04-04T12:20:21.233 回答