-5

如何通过在 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());
}
4

1 回答 1

0

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

如果我是对的: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);

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

于 2013-09-19T00:01:50.257 回答