Erreur logCat "java.lang.RuntimeException"
Salut
je suis entrain de faire un traitement avec la base de données SQLite et je suis débutante.
Je vous explique ce que j'essaye de faire:
Quand l'utilisateur sélectionne la ville et clique sur le bouton recherche, la recherche se lance pour trouver la formation qui se trouve dans cette ville à partir de la base de données.
Code:
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 32 33 34 35 36 37 38
| 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 DATABASE_NAME = "formation.db";
private static final int DATABASE_VERSION = 1;
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(final Context context) {
super(context, DATABASE_NAME, null, DATABASE_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);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//On peut faire ce qu'on veut ici moi j'ai décidé de supprimer la table et de la recréer
//comme ça lorsque je change la version les id repartent de 0
Log.i(getClass().getName(), "Upgrationg database from version " + oldVersion + " to " +newVersion);
db.execSQL("DROP TABLE " + TABLE_FORMATION + ";");
onCreate(db);
}
} |
Code:
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| public final class FormationBDD {
private static FormationBDD instance = new FormationBDD();
private transient MaBase maBaseSQLite;
private transient SQLiteDatabase bdd;
private static final String TABLE_FORMATION = "table_formation";
private static final String COL_ID = "ID";
private static final int NUM_COL_ID = 0;
private static final String COL_LIB = "libellé";
private static final int NUM_COL_LIB = 1;
private static final String COL_TEL = "tel";
private static final int NUM_COL_TEL = 2;
private static final String COL_EMAIL = "email";
private static final int NUM_COL_EMAIL = 3;
private static final String COL_ADR = "adresse";
private static final int NUM_COL_ADR = 4;
private static final String COL_VILLE = "ville";
private static final int NUM_COL_VILLE = 5;
private static final String COL_CAT= "catégorie";
private static final int NUM_COL_CAT = 6;
private FormationBDD(){
super();
}
public static FormationBDD getInstance(){
return instance;
}
public void initialize(final Context context){
//On crée la BDD et sa table
this.maBaseSQLite = new MaBase(context);
}
public void open()throws SQLiteException{
//on ouvre la BDD en écriture
bdd = maBaseSQLite.getWritableDatabase();
}
public void close(){
//on ferme l'accès à la BDD
bdd.close();
}
public SQLiteDatabase getBDD(){
return bdd;
}
public long insertFormation(Formation formation){
ContentValues values = new ContentValues();
//on lui ajoute une valeur associée à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
values.put(COL_LIB, formation.getLibellé());
values.put(COL_TEL, formation.getTel());
values.put(COL_EMAIL, formation.getEmail());
values.put(COL_ADR, formation.getAdresse());
values.put(COL_VILLE, formation.getVille());
values.put(COL_CAT, formation.getCatégorie());
//on insère l'objet dans la BDD via le ContentValues
return bdd.insert(TABLE_FORMATION, null, values);
public Formation getFormation(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.rawQuery(" select " + COL_LIB + " from " + TABLE_FORMATION + " where " + COL_CAT + " > ?", new String[] {catégorie});
return cursorToFormation(c);
}
} |
Code:
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
public class Main extends Activity {
private Spinner listeVille = null;
private Spinner listeCat = null;
private Button rechercheBout = null;
Formation liste;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recherche);
FormationBDD.getInstance().initialize(this);
//Création d'une instance de ma classe FormationBDD
FormationBDD.getInstance().open();
// on ouvre la base de données
Formation formation = new Formation("coaching life", "20125885", "hhfsjkk", "adresse", "tunis", "gestion");
FormationBDD.getInstance().insertFormation(formation);
listeVille = (Spinner) findViewById(R.id.spinnerVille);
List<String> exemple = new ArrayList<String>();
exemple.add("Tunis");
exemple.add("Sfax");
exemple.add("Sousse");
listeCat = (Spinner) findViewById(R.id.spinnerCat);
List<String> catégorieList = new ArrayList<String>();
catégorieList.add("Informatique");
catégorieList.add("gestion");
catégorieList.add("Langue");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, exemple);
//Le layout par défaut est android.R.layout.simple_spinner_dropdown_item
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listeVille.setAdapter(adapter);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, catégorieList);
//Le layout par défaut est android.R.layout.simple_spinner_dropdown_item
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listeCat.setAdapter(adapter1);
rechercheBout = (Button)findViewById(R.id.recherche);
String catégorie = listeCat.getSelectedItem().toString();
String ville = listeVille.getSelectedItem().toString();
liste = FormationBDD.getInstance().getFormation(catégorie);
rechercheBout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (liste == null)
Toast.makeText(Main.this, "liste de formation vide ", Toast.LENGTH_LONG).show();
else
Toast.makeText(Main.this, "liste de formation " + liste.getCatégorie(), Toast.LENGTH_LONG).show();
}
}); |
Code:
1 2 3
|
12-09 00:42:24.700: W/dalvikvm(8648): threadid=1: thread exiting with uncaught exception (group=0x40c6c1f8)
12-09 00:42:24.705: E/AndroidRuntime(8648): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.trainingcenter/com.example.trainingcenter.Main}: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. |
Merci pour l'aide