Ale1794 Ale1794 - 4 months ago 5x
Android Question

Android SQL Query syntax

I use this java class to create a database:

public class DBhelper extends SQLiteOpenHelper {

private static final String DB_NAME = "grades.db";
private static final int DB_VERSION = 1;
public static final String TABELLA_RICETTE = "TabellaRicette";
public static final String COL_ID="ColonnaId";
public static final String COL_NOME="ColonnaNome";
public static final String COL_TIPO = "ColonnaTipo";
public static final String COL_IMMAGINE = "ColonnaImmagine";
public static final String COL_ATTRIBUTI="ColonnaAttributi";
public static final String COL_INGREDIENTI = "ColonnaIngredienti";
public static final String COL_DIFFICOLTA="ColonnaDifficolta";
public static final String COL_DESCRIZIONE="ColonnaDescrizione";

public DBhelper(Context context){
super(context, DB_NAME, null, DB_VERSION);

public void onCreate(SQLiteDatabase db) {
String sql_tab = "create table " + TABELLA_RICETTE + "( " +
COL_ID + " integer primary key autoincrement, " +
COL_NOME + " text not null, " +
COL_TIPO + " text not null, " +
COL_IMMAGINE + " text not null, " +
COL_ATTRIBUTI + " text not null, " +
COL_INGREDIENTI + " text not null, " +
COL_DIFFICOLTA + " text not null, " +
COL_DESCRIZIONE + " text not null " +

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

I use this function to obtain all the information in the database:

public Cursor get_all() {
return getWritableDatabase().query(TABELLA_RICETTE, null, null, null, null, null, null);

Now i would make a more specific query, selecting all the element that has a particular
String s1
String s2

I tried to write something, but maybe i am wrong in syntax too.

Can you help me with this function?

public Cursor get_something(String s1,String s2) {
String whereClause="COL_TIPO=? AND COL_DIFFICOLTA=?";
String[] whereArgs=new String[] {s1,s2};
return getWritableDatabase().query(TABELLA_RICETTE,null,null,whereClause,whereArgs,null,null,null);


COL_TIPO is not the name of a column but the name of the symbol that contains the name of the column.

You have to put the symbols' values into the SQL string:

String whereClause = COL_TIPO+"=? AND "+COL_DIFFICOLTA+"=?";