Salut
j'ai un problème dont je ne comprends vraiment pas la raison!
En fait, j'ai une table qui contient 7 colonnes: id, Libellé, tel, email, adresse, ville, catégorie. J'ai bien créé la fonction d'ajout d'un objet à cette table mais pour la colonne catégorie, lorsque je teste, j'ai ce resultat:
colonne ville reçoit ce que devra etre le contenu de la colonne catégorie et la colonne catégorie reçoit null!! (sachant que lorsque j'enlève la colonne catégorie, tout fonctionne correctement, càd chaque colonne reçoit l'instanciation correspondante)
J'ai vérifié toutes les déclarations d'attributs, la création de la table dans la base de données ainsi que la fonction d'insertion sans trouver aucune faute! est ce un problème interne au fonctionnement????
NB: je teste l'application sur un téléphone non pas émulateur
merci d'avance pour votre aide.
voilà les bout de codes:
classe Formation: (qui contient la déclarations des attributs)
classe FormationBDD: (gère l'ajout, suppression et modification)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 package com.example.trainingcenter; public class Formation { private int id; private String libellé; private String tel; private String email; private String adresse; private String ville; private String catégorie; //private String catégorie; public Formation(){ super(); } public Formation(String libellé, String tel, String email, String adresse, String ville, String catégorie){ super(); this.setLibellé(libellé); this.setTel(tel); this.setEmail(email); this.setAdresse(adresse); this.setVille(ville); this.setCatégorie(catégorie); //this.setCatégorie(catégorie); // manque l'attribut pour les catégories de formation qui doit etre une sorte de tableau ou liste (a vérifier) } public void setId(int id) { this.id = id; } public int getId() { return id; } public void setLibellé(String libellé) { this.libellé = libellé; } public String getLibellé() { return libellé; } public void setTel(String tel) { this.tel = tel; } public String getTel() { return tel; } public void setEmail(String email) { this.email = email; } public String getEmail() { return email; } public void setAdresse(String adresse) { this.adresse = adresse; } public String getAdresse() { return adresse; } public String toString(){ return "ID : "+id+"\nLibellé : "+libellé+"\nTel : "+tel+"\nEmail : "+email+"\nAdresse : "+adresse+"\nVille : "+ville+"\nCatégorie : "+catégorie; } public void setVille(String ville) { this.ville = ville; } public String getVille() { return ville; } public void setCatégorie(String catégorie) { this.catégorie = catégorie; } public String getCatégorie() { return catégorie; } }
NB: j'ai copié juste ce qui concerne l'ajout
La classe MaBase: création de la base et la table
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
77
78
79
80 public final class FormationBDD { private static final int VERSION_BDD = 1; private static final String NOM_BDD = "formations.db"; 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 SQLiteDatabase bdd; private MaBase maBaseSQLite; public FormationBDD(Context context){ //On créer la BDD et sa table maBaseSQLite = new MaBase(context, NOM_BDD, null, VERSION_BDD); } public void open(){ //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); } private Formation cursorToFormation(Cursor c){ if (c.getCount() == 0) return null; c.moveToFirst(); Formation formation = new Formation(); formation.setId(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.setVille(c.getString(NUM_COL_CAT)); //On ferme le cursor c.close(); return formation; } }
et enfin le main:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.recherche); //Création d'une instance de ma classe FormationBDD FormationBDD formationBdd = new FormationBDD(this); // on ouvre la base de données Formation formation = new Formation("libellé", "tel", "email", "adresse", "ville", "catégorie"); formationBdd.open(); formationBdd.insertFormation(formation); Formation formationFromBdd = formationBdd.getFormationWithTitre(formation.getLibellé()); if(formationFromBdd != null){ Toast.makeText(this, formationFromBdd.toString(), Toast.LENGTH_LONG).show(); } else{ Toast.makeText(this, "la formation n'existe pas", Toast.LENGTH_LONG).show(); } formationBdd.close();
Partager