Cin Cin - 2 months ago 10
Android Question

How to search a String value in Sqlite?

I am working on a project and I have little understanding in sqlite. I am working on android application for searching a name from sqlite local database.
I want to grap a text (name) from the editText box and then do query in sqlite.
I found out how to grap a text from the editText box -

EditText txtname = (EditText)findViewById(R.id.name);
String name = txtname.getText().toString();


From my research, I found this is how you do query in sqlite -

SELECT * FROM Customers
WHERE City LIKE '%XXXX%';


Since, I don't know what user will search in the editText box.

How can I do query that user entered in the editText box to the sqlite database.

Thanks

Answer

I'm not sure what you are asking but I guess it's how to pass the name to the query.

Use SQLiteDatabase.query() with selection arguments, for example

db.query("Customers", null, "City LIKE ?", new String[] { "%" + name + "%" }, null, null, null);