我无法从sqlite获取特定数据?请帮我

时间:2014-03-21 05:40:46

标签: android

![这是我在DBHelper中查找方法的代码] .class [1]

public StringBuilder findData(String find)
{    
    StringBuilder output = new StringBuilder("Our data is:\n");
    Cursor c = ourHelper.getReadableDatabase().rawQuery("SELECT abbrevaition FROM        abbrtab WHERE acronym"+" =  ?  ;", new String[]{find});
    String remarks=" ";
    int iRemarks = c.getColumnIndex(KEY_ABBR);
    if( c.moveToFirst() && c.getCount()>0 )
    {
        remarks= c.getString(iRemarks);
    if(remarks.isEmpty())
        {
            Toast.makeText(ourContext, "not", Toast.LENGTH_LONG).show();
        }
    output.append(remarks); 
    }
    c.close();
    return output;
}

![这是Main.java中用于从SQLite3获取数据的代码。当我从abbrtab给出查询select *然后它执行。并应用where条件然后我没有得到任何东西。 ] [1]

@Override
public void onClick(View v) 
{
    Toast.makeText(getApplicationContext(), String.valueOf("Hi"),Toast.LENGTH_LONG).show();
    // TODO Auto-generated method stub
    ASNDBAdapter info = new ASNDBAdapter(AllBranches.this);
    info.open();
    String data = et.getText().toString();
    String result = info.findData(data).toString();
    Intent i = new Intent(AllBranches.this, ViewData.class);
    i.putExtra("my_data", result);
    startActivity(i);
    Toast.makeText(getApplicationContext(), String.valueOf(result),
                            Toast.LENGTH_LONG).show();
    info.close();

}

2 个答案:

答案 0 :(得分:3)

public StringBuilder findData(String find)
    {    
        StringBuilder output = new StringBuilder("Our data is:\n");
        Cursor c = ourHelper.getReadableDatabase().query("abbrtab", new String[] {"abbrevaition"},"acronym" +"="+find, null, null, null, null);
        String remarks=" ";
        int iRemarks = c.getColumnIndex(KEY_ABBR);
        if( c.moveToFirst() && c.getCount()>0 )
        {
            remarks= c.getString(iRemarks);
        if(remarks.isEmpty())
            {
                Toast.makeText(ourContext, "not", Toast.LENGTH_LONG).show();
            }
        output.append(remarks); 
        }
        c.close();
        return output;
    }

这里" abbrtab"是表名和" abbrevaition"是字段的名称。

答案 1 :(得分:1)

要在查询中获取参数值,您必须使用?之类的parameter token

String[] params = new String[]{ find };
Cursor c = db.rawQuery("SELECT abbrevaition FROM abbrtab WHERE acronym = ?",params);

对于像这样的简单查询,您不需要使用rawQuery,但可以使用query

String[] params = new String[]{ find };
Cursor c = db.query("abbrtab ", new String{"abbrevaition"},
                "acronym = ?", params,
                null, null, null);

您还必须确保Column Names正确无误。

尝试这种方式。