Android - 查询字符串而不是整数

时间:2010-10-11 12:28:01

标签: android sqlite

我正在尝试查询字符串(数据库中的TEXT字段),并希望根据该查询获取记录。我正在按照一个教程,如果我查询整数(_ID字段),它可以正常工作。我需要在下面的代码中更改什么才能让它搜索字符串(TEXT字段)而不是_ID字段?

活动代码:

protected void getDetails() {
    try
    {
        // The ArrayList that holds the row data
        ArrayList<Object> row;
        // ask the database manager to retrieve the row with the given rowID
        row = db.getRowAsArray(Long.parseLong(txtProjContacts.getText().toString()));

        // update the form fields to hold the retrieved data
        txtName.setText((String)row.get(1));
        txtEmail.setText((String)row.get(2));
        txtExpertise.setText((String)row.get(3));
        txtCharges.setText((String)row.get(4));
    }
    catch (Exception e)
    {
        Log.e("Retrieve Error", e.toString());
        e.printStackTrace();
    }

}

数据库适配器代码:

public ArrayList<Object> getRowAsArray(Long rowID)
{
    // create an array list to store data from the database row.
    // I would recommend creating a JavaBean compliant object 
    // to store this data instead.  That way you can ensure
    // data types are correct.
    ArrayList<Object> rowArray = new ArrayList<Object>();
    Cursor cursor;

    try
    {
        // this is a database call that creates a "cursor" object.
        // the cursor object store the information collected from the
        // database and is used to iterate through the data.
        cursor = db.query
        (
                TABLE_CONTACTS,
                new String[] {     TABLE_CON_ID,
                                TABLE_CON_NAME,
                                TABLE_CON_EMAIL,
                                TABLE_CON_EXPERTISE,
                                TABLE_CON_CHARGES},
                TABLE_CON_ID + "=" + rowID,
                null, null, null, null, null
        );

        // move the pointer to position zero in the cursor.
        cursor.moveToFirst();

        // if there is data available after the cursor's pointer, add
        // it to the ArrayList that will be returned by the method.
        if (!cursor.isAfterLast())
        {
            do
            {
                rowArray.add(cursor.getLong(0));
                rowArray.add(cursor.getString(1));
                rowArray.add(cursor.getString(2));
                rowArray.add(cursor.getString(3));
                rowArray.add(cursor.getString(4));
            }
            while (cursor.moveToNext());
        }

        // let java know that you are through with the cursor.
        cursor.close();
    }
    catch (SQLException e) 
    {
        Log.e("DB ERROR", e.toString());
        e.printStackTrace();
    }

    // return the ArrayList containing the given row from the database.
    return rowArray;
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

您需要确保选择条件是否包含在引号内。

未经测试的代码

cursor = db.query
        (
                TABLE_CONTACTS,
                new String[] {     TABLE_CON_ID,
                                TABLE_CON_NAME,
                                TABLE_CON_EMAIL,
                                TABLE_CON_EXPERTISE,
                                TABLE_CON_CHARGES},
                TABLE_CON_NAME + "= \"" + name_to_be_searched + "\"",
                null, null, null, null, null
        );

但这会导致SQL注入的可能性,因此请使用SQLLiteQuery