SQlite DB搜索在哪里

时间:2013-09-18 20:30:31

标签: android sqlite

如何使用EditText编写的名称搜索数据库?

我的代码:

SQLiteDatabase vt = VtArac.getReadableDatabase();
Cursor VTYeri = vt.query(DatabaseHelper.VT_TABLO, new String[]{ "Id", "banka", "sira", "kod", "aktif", "rea", "kap", "Ryzde", "Kyzde"}, "Id" + "=" + "1", null, null, null,null, null);
startManagingCursor(VTYeri);
while(VTYeri.moveToNext()) {
    String Id_Degiskeni = VTYeri.getString((VTYeri.getColumnIndex("Id")));
    IDDD.setText(Id_Degiskeni.toString());
    String banka_Degiskeni = VTYeri.getString((VTYeri.getColumnIndex("banka")));
    editText1.setText(banka_Degiskeni.toString());
    String sira_Degiskeni = VTYeri.getString((VTYeri.getColumnIndex("sira")));
    editText2.setText(sira_Degiskeni.toString());
    String kod_Degiskeni = VTYeri.getString((VTYeri.getColumnIndex("kod")));
    editText3.setText(kod_Degiskeni.toString());
    String aktif_Degiskeni = VTYeri.getString((VTYeri.getColumnIndex("aktif")));
    aktif.setText(aktif_Degiskeni.toString());
    String rea_Degiskeni = VTYeri.getString((VTYeri.getColumnIndex("rea")));
    reaktif.setText(rea_Degiskeni.toString());
    String kap_Degiskeni = VTYeri.getString((VTYeri.getColumnIndex("kap")));
    kapasitif.setText(kap_Degiskeni.toString());
    String Ryzde_Degiskeni = VTYeri.getString((VTYeri.getColumnIndex("Ryzde")));
    Ryuzde.setText(Ryzde_Degiskeni.toString());
    String Kyzde_Degiskeni = VTYeri.getString((VTYeri.getColumnIndex("Kyzde")));
    Kyuzde.setText(Kyzde_Degiskeni.toString());
}

1 个答案:

答案 0 :(得分:0)

当前查询为“Id = 1”。您想要根据EditText中的文本进行查询。这是对的吗?

如果我是对的: reference documentation for SQLiteDatabase.query()是指南:

而不是进入 "Id" + "=" + "1" 加上这个:

EditText myEditText = (EditText) findViewById(<resId>);
String enteredText = mEditText.getText().toString();
String[] args = new String[1];
args[0] = enteredText;

对于查询,请使用

Cursor VTYeri = vt.query(DatabaseHelper.VT_TABLO, new String[]{ "Id", "banka", "sira", "kod", "aktif", "rea", "kap", "Ryzde", "Kyzde"}, "Id" + " = ?", args, null, null,null, null);

这会将enteredText的值绑定到您的选择语句“Id =”。参数绑定是进行查询的唯一安全方式;连接使您对SQL注入开放,这可能非常危险。即使这样,您也应该通过自己的输入验证来添加我的代码。