2016-06-16 9 views
0

ich diese Java-Klasse verwenden, um eine Datenbank zu erstellen:Android SQL Query Syntax

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

Ich benutze diese Funktion alle Daten in der Datenbank zu erhalten:

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

Jetzt würde ich eine mehr machen spezifische Abfrage, Auswahl aller Elemente, die eine bestimmte String s1 in COL_TIPO und String s2 in hat.

Ich habe versucht, etwas zu schreiben, aber vielleicht bin ich auch falsch in der Syntax.

Können Sie mir bei dieser Funktion helfen?

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

Was genau ist das Problem? –

Antwort

1

COL_TIPO ist nicht der Name einer Spalte, aber der Name des Symbols, das den Namen der Spalte enthält.

Sie müssen die Werte in die SQL-Zeichenfolge Symbole setzen:

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