代码之家  ›  专栏  ›  技术社区  ›  Siddharth Lele

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

  •  0
  • Siddharth Lele  · 技术社区  · 14 年前

    我试图查询一个字符串(数据库中的文本字段),并希望根据该查询获取记录。我正在学习一个教程,如果我查询一个整数(ID字段),这个教程会很好地工作。我需要在下面的代码中更改什么,让它搜索字符串(文本字段)而不是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 回复  |  直到 14 年前
        1
  •  0
  •   st0le    14 年前

    你需要确保你选择的条件是这样的,它被括在引号里。

    未经测试的代码

    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
            );
    

    SQLLiteQuery