0

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);
}

@Override
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 " +
            ");";
    db.execSQL(sql_tab);
}

@Override
   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 in COL_TIPO and String s2 in COL_DIFFICOLTA.

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);
}
1
  • So what exactly is the problem here? Commented Jun 16, 2016 at 15:55

1 Answer 1

1

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+"=?";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.