2

我有一个 sqlite 数据库,我想检索特定的数据列并将其存储到字符串数组中。数据库内部有两列。会有多行具有相同用户名的数据,我想检索用户的“ContentPath”并将其存储到字符串数组中。但我不知道如何检索该特定列数据...

    public String[] get_contentByEmailID(String emailid){
    String[] returnMsg = null;
    helper = this.getReadableDatabase();

    Cursor c = helper.rawQuery("SELECT tableid, emailid, contentpath" +
            " from w_content where emailid='"+emailid"' ", null);



    int contentpathColumn = c.getColumnIndex("contentpath");


    if (c.moveToFirst()) {
        do {
            returnMsg = new String[2]; 

            String contentpath = c.getString(contentpathColumn);

            returnMsg[0] = emailid_sync;

            returnMsg[1] = contentpath;


        } while (c.moveToNext());
    }
    if (c != null && !c.isClosed()) {
        c.close();
    }
    if (helper!=null){
        helper.close();
    };
    return returnMsg;
}

当我调用这个函数来检索数据时。它提供了 emailid 和 contentpath。

String values[] = helper.get_contentByEmailID(SettingConstant.EMAIL);

任何意见将不胜感激。

4

1 回答 1

1

数组填充 emailid 和 contentpath 的原因是,因为你总是重置returnMsg每一行并用这样的值填充它。由于会有不同的行数,因此通常建议您使用ArrayList,而不是构建静态长度数组。

要修复它,请更改:

String[] returnMsg = null;

到:

ArrayList<String> returnMsg = new ArrayList<String>();

然后,在您的 中do{},执行以下操作:

do {
    String contentpath = c.getString(contentpathColumn);
    returnMsg.add(contentpath);
} while (c.moveToNext());

最后,将您的 return 语句更改为:

return returnMsg.toArray();
于 2012-12-10T02:35:15.967 回答