code de la fonction qui doit retourner la formation selon paramètre ville et catégorie:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public Formation getFormation(String ville, String catégorie){
//Récupère dans un Cursor les valeurs correspondant à un livre contenu dans la BDD (ici on sélectionne le livre grâce à son titre)
Cursor c = bdd.query(TABLE_FORMATION, new String[] {COL_ID, COL_LIB, COL_TEL, COL_EMAIL, COL_ADR, COL_VILLE, COL_CAT}, COL_VILLE + " LIKE \"" + ville +"\" and \"" + COL_CAT + " LIKE \"" + catégorie +"\"", null, null, null, null);
return cursorToFormation(c);
}
private Formation cursorToFormation(Cursor c){
if (c.getCount() == 0)
return null;
c.moveToFirst();
Formation formation = new Formation();
formation.set_id(c.getInt(NUM_COL_ID));
formation.setLibellé(c.getString(NUM_COL_LIB));
formation.setTel(c.getString(NUM_COL_TEL));
formation.setEmail(c.getString(NUM_COL_EMAIL));
formation.setAdresse(c.getString(NUM_COL_ADR));
formation.setVille(c.getString(NUM_COL_VILLE));
formation.setCatégorie(c.getString(NUM_COL_CAT));
//On ferme le cursor
c.close();
return formation;
} |
la table formation dans base de données
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class MaBase extends SQLiteOpenHelper {
private static final String TABLE_FORMATION = "table_formation";
private static final String COL_ID = "_id";
private static final String COL_LIB = "libellé";
private static final String COL_TEL = "tel";
private static final String COL_EMAIL = "email";
private static final String COL_ADR = "adresse";
private static final String COL_VILLE = "ville";
private static final String COL_CAT = "catégorie";
private static final String CREATE_BDD = "CREATE TABLE " + TABLE_FORMATION + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_LIB + " TEXT NOT NULL, "
+ COL_TEL + " TEXT NOT NULL, " + COL_EMAIL + " TEXT NOT NULL, " + COL_ADR + " TEXT NOT NULL, " + COL_VILLE + " TEXT NOT NULL, "+ COL_CAT + " TEXT NOT NULL);";
public MaBase(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//on crée la table à partir de la requête écrite dans la variable CREATE_BDD
db.execSQL(CREATE_BDD);
}
} |
la fonction qui est écris dans le main qui doit normalement géré le clique sur le bouton "recherche" afin de retrouver la formation recherchée par l'utilisateur selon les paramètres ville et catégorie qu'il a choisis.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String catégorie = listeCat.getSelectedItem().toString();
String ville = listeVille.getSelectedItem().toString();
liste = formationBdd.getFormation(ville, catégorie);
if (liste == null)
Toast.makeText(Main2.this, "liste de formation vide ", Toast.LENGTH_LONG).show();
else
Toast.makeText(Main2.this, "liste de formation " + liste.getCatégorie(), Toast.LENGTH_LONG).show();
}
} |
le rapport logCat:
12-12 22:21:59.365: E/AndroidRuntime(20556): android.database.sqlite.SQLiteException: near "Informatique": syntax error: , while compiling: SELECT _id, libellé, tel, email, adresse, ville, catégorie FROM table_formation WHERE ville LIKE "Tunis" and "catégorie LIKE "Informatique"
Lorsque je clique sur le bouton "recherche" l'application s'arrete
Partager