I have a precompiled database and I want to get the data according the passed parameters
But for some strange reason the cursor results always empty
My getData method in the adapter is
public Cursor getData(String uso, String eta, String category,
String class) {
try {
Cursor c;
if (class.equals(null) && category.equals(null)) {
c = mDb.rawQuery("SELECT * FROM dbtable WHERE Uso LIKE \""
+ uso + "\" AND Eta LIKE \"" + eta
+ "\" AND Language LIKE \"" + language
+ "\" order by Name asc", null);
} else if (class.equals(null)) {
c = mDb.rawQuery("SELECT * FROM dbtable WHERE Uso LIKE \""
+ uso + "\" AND Eta LIKE \"" + eta
+ "\" AND Tipo LIKE \"" + category
+ "\" AND Language LIKE \"" + language
+ "\" order by Name asc", null);
} else if (category.equals(null)) {
c = mDb.rawQuery("SELECT * FROM dbtable WHERE Uso LIKE \""
+ uso + "\" AND Eta LIKE \"" + eta
+ "\" AND Certification LIKE \"" + class
+ "\" AND Language LIKE \"" + language
+ "\" order by Name asc", null);
} else {
c = mDb.rawQuery("SELECT * FROM dbtable WHERE Uso LIKE \""
+ uso + "\" AND Eta LIKE \"" + eta
+ "\" AND Tipo LIKE \"" + category
+ "\" AND Certification LIKE \"" + class
+ "\" AND Language LIKE \"" + language
+ "\" order by Name asc", null);
}
if (c != null) {
c.moveToNext();
}
return c;
} catch (SQLException mSQLException) {
Log.e(TAG, "getDataCar >>" + mSQLException.toString());
throw mSQLException;
}
}
...
This cursor is used in a method that puts all entries in an array of Car objects
public Car[] getDataDBCarsArray(String uso, String eta,
String category, String class) {
ArrayList<Car> list = new ArrayList<Car>();
Cursor c = getData(uso, eta, category, class);
try {
.....
All works fine If instead of the above getData(….) I try to use
public Cursor getAllData(){
try {
Cursor c = mDb.rawQuery("SELECT * FROM dbtable order by Name asc", null);
if (c != null) {
c.moveToNext();
}
return c;
}catch (SQLException mSQLException) {
throw mSQLException;
}
}
so seems a problem with the query in getData(…).
But I cannot figure where is the error.
If I try to open the database there are many elements for the passed parameters but strangely doesn’t find anything.
NB
language is a static value that depends by the device language
You have forgotten the SQL wildcards…
This:
Is the same as:
You want to incorporate
%and_wildcards to create a clause like:Also you should take advantage of SQLite parametrization and protect yourself from SQL injection attacks: