I have a SQlite database with some columns and with a content of an EditText I would like to select some rows from the database.
In main activity:
case R.id.betx:
String dates2 = sqletx.getText().toString();
GlobalVars.settables(dates2);
Intent intentstar2t = new Intent(dbhelp.this, CustomList.class);
startActivity(intentstar2t);
break;
This is my main activity, the ‘dates2’ String is the content of the edittext, and I save it for the setTables method.
My code in the hornot class where:
public Cursor getTable() {
String deda = GlobalVars.gettables();
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS,
KEY_CALORIE, KEY_MULTI, KEY_DATE};
return ourDatabase.query(DATABASE_TABLE, columns, KEY_DATE + "=" + deda ,
null, null, null, null);
}
Here I create a Cursor for the rows that fulfill the requirements in the last row. The String deda will be the ‘dates2’ we set before and I would like to get the rows where the KEY_DATE is equivalent to dates2.
The listview activity where I list the suitable rows.
if (todoItems.size() > 0) {
todoItems.clear();
}
if (list.size() > 0) {
list.clear();
}
Cursor c = info.getTable();
c = info.getTable();
if (c.moveToFirst())
{
do{
todoItems.add(c.getString(0) + " " + c.getString(1) + " " + c.getString(2)+ " " + c.getString(3)+ " " + c.getString(4));
quanitems.add(c.getString(4));
}
while (c.moveToNext());
}
if (todoItems.size() > 0)
{
for (int i=0; i<todoItems.size(); i++)
{
each=new EachRow();
each.text=(String) todoItems.get(i);
list.add(each);
each2=new EachRow();
each2.text=(String) quanitems.get(i);
list2.add(each2);
}
listView.setAdapter(new MyAdapter(this, 0, list));
}
I use a custom listview, in every row there are some items, but the point is that I create a cursor (c) for the rows where the KEY_DATE is equivalent to dates2.
My problem is I never define integer variable here, but if I create an entry where the date is for example “107” or “6519684” it is working, but if it is “tableOne” or any text it crushes at the row:
return ourDatabase.query(DATABASE_TABLE, columns, KEY_DATE + "=" + deda , null, null, null, null);
and
Cursor c = info.getTable();
(as you can see both problems are connected to the getTable method.
I use only Strings so practically the numbers (“107”) are strings as well.
Any idea?
Thanks in advance!
use:
return ourDatabase.query(DATABASE_TABLE, columns, KEY_DATE + "=?" ,new String[] { deda }, null, null, null);
Edit(explaination):
in
querymethod you haveString selectionandString[] selectionArgsparameters, so inselectionyou can use?for parameters inselectionArgs. Now underlying query builder will build query with this paramater and will apply proper escape chars.