Bonjour,
je voulais inserer une colonne dans une table d'une base sqlite sous android:
// creation de la table Projet:
Ma classe qui gère l’interaction avec la BD:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 private static final String TABLE_PROJET = "table_projet"; private static final String COL_IDPROJET = "IDP"; private static final String COL_NOM_PROJET = "nom_projet"; private static final String COL_DATED_PROJET = "dated"; private static final String COL_DATEF_PROJET = "datef"; private static final String CREATE_PROJET = "CREATE TABLE " + TABLE_PROJET + "(" + COL_IDPROJET + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_NOM_PROJET + " TEXT NOT NULL, " + COL_DATED_PROJET + " INTEGER NOT NULL, " + COL_DATEF_PROJET + " INTEGER NOT NULL );";
Main activity:
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
93
94
95
96
97
98
99
100
101
102
103 public class ProjetDB { private static final int VERSION_BDD = 3; private static final String NOM_BDD = "projet.db"; private static final String TABLE_PROJET = "table_projet"; private static final String COL_IDPROJET = "IDP"; private static final int NUM_COL_ID =0; private static final String COL_DATED_PROJET = "dated"; private static final int NUM_COL_DATED =1; private static final String COL_DATEF_PROJET = "datef"; private static final int NUM_COL_DATEF =2; private static final String COL_NOM_PROJET = "nom_projet"; private static final int NUM_COL_NOMP =2; private SQLiteDatabase Pr_bd; private DataBaseClasse maBaseProjet; public ProjetDB(Context context){ //On crée la BDD et sa table maBaseProjet = new DataBaseClasse(context, NOM_BDD, null, VERSION_BDD); } public void open(){ //on ouvre la BDD en écriture Pr_bd = maBaseProjet.getWritableDatabase(); } public void close(){ //on ferme l'accès à la BDD Pr_bd.close(); } public SQLiteDatabase getBDD(){ return Pr_bd; } public long insertprojet( MesProjets projet){ //Création d'un ContentValues (fonctionne comme une HashMap) 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_NOM_PROJET, MesProjets.getNomP()); values.put(COL_DATED_PROJET, MesProjets.getDateD()); values.put(COL_DATEF_PROJET, MesProjets.getDateF()); //on insère l'objet dans la BDD via le ContentValues return Pr_bd.insert(TABLE_PROJET, null, values); } public int updateprojet(int id, MesProjets projet){ //La mise à jour d'une note dans la BDD fonctionne plus ou moins comme une insertion //il faut simplement préciser quel note on doit mettre à jour grâce à l'ID ContentValues values = new ContentValues(); values.put(COL_NOM_PROJET, MesProjets.getNomP()); values.put(COL_DATED_PROJET, MesProjets.getDateD()); values.put(COL_DATEF_PROJET, MesProjets.getDateF()); return Pr_bd.update(TABLE_PROJET, values, COL_IDPROJET + " = " +id, null); } public int removeNoteWithID(int id){ //Suppression d'un livre de la BDD grâce à l'ID return Pr_bd.delete(TABLE_PROJET, COL_IDPROJET + " = " +id, null); } public MesProjets getprojetWithNom(String nom){ //Récupère dans un Cursor les valeurs correspondant à un projet contenu dans la BDD (ici on sélectionne la note grâce à son titre) Cursor c = Pr_bd.query(TABLE_PROJET, new String[] {COL_IDPROJET, COL_DATED_PROJET, COL_DATEF_PROJET, COL_NOM_PROJET, }, COL_NOM_PROJET + " LIKE \"" + nom +"\"", null, null, null, null); return cursorToLivre(c); } public MesProjets getprojetWithDateD(int dated){ //Récupère dans un Cursor les valeurs correspondant à un projet contenu dans la BDD (ici on sélectionne la note grâce à son titre) Cursor c = Pr_bd.query(TABLE_PROJET, new String[] {COL_IDPROJET, COL_DATED_PROJET, COL_DATEF_PROJET, COL_NOM_PROJET, }, COL_DATED_PROJET + " LIKE \"" + dated +"\"", null, null, null, null); return cursorToLivre(c); } public MesProjets getprojetWithDateF(int datef){ //Récupère dans un Cursor les valeurs correspondant à un projet contenu dans la BDD (ici on sélectionne la note grâce à son titre) Cursor c = Pr_bd.query(TABLE_PROJET, new String[] {COL_IDPROJET, COL_DATED_PROJET, COL_DATEF_PROJET, COL_NOM_PROJET, }, COL_DATEF_PROJET + " LIKE \"" + datef +"\"", null, null, null, null); return cursorToLivre(c); } //Cette méthode permet de convertir un cursor en un projet private MesProjets cursorToLivre(Cursor c){ //si aucun élément n'a été retourné dans la requête, on renvoie null if (c.getCount() == 0) return null; //Sinon on se place sur le premier élément c.moveToFirst(); //On créé un livre MesProjets mprojet = new MesProjets(); //on lui affecte toutes les infos grâce aux infos contenues dans le Cursor Note.setId(c.getInt(NUM_COL_ID)); MesProjets.setNomP(c.getString(NUM_COL_NOMP)); MesProjets.setDateD(c.getInt(NUM_COL_DATED)); MesProjets.setDateF(c.getInt(NUM_COL_DATEF)); //On ferme le cursor c.close(); //On retourne le livre return mprojet; } }
lorsque j'execute ce programme l'emulateur m'affiche: NomPackage.MesProjets@41075920 dans un taost, puis Ce projet n'existe pas dans la 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
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 public class MainProjet extends Activity { private static final String TAG = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.projet_activity); //Création d'une instance de ma classe NoteDB ProjetDB maprojetDB = new ProjetDB(this); //Création d'une note MesProjets pr = new MesProjets("titre", 2015, 2016,1); //On ouvre la base de données pour écrire dedans maprojetDB.open(); //On insère la note que l'on vient de créer maprojetDB.insertprojet(pr); String log= "success"; Log.d(TAG, log); //Pour vérifier que l'on a bien créé notre note dans la BDD //on extrait la note de la BDD grâce au titre du livre que l'on a créé précédemment MesProjets projetFromBdd = maprojetDB.getprojetWithNom(pr.getNomP()); if(projetFromBdd != null){ //On affiche les infos du note dans un Toast Toast.makeText(this, projetFromBdd.toString(), Toast.LENGTH_LONG).show(); projetFromBdd.setNomP("J'ai modifié le nom du projet"); //Puis on met à jour la BDD maprojetDB.updateprojet(projetFromBdd.getId(), projetFromBdd); } //On extrait la note de la BDD grâce au nouveau titre projetFromBdd = maprojetDB.getprojetWithNom("J'ai modifié le nom du projet"); //S'il existe une note possédant ce titre dans la BDD if(projetFromBdd != null){ //On affiche les nouvelles informations du note pour vérifier que le titre du livre a bien été mis à jour Toast.makeText(this, projetFromBdd.toString(), Toast.LENGTH_LONG).show(); //on supprime la note de la BDD grâce à son ID maprojetDB.removeNoteWithID(projetFromBdd.getId()); } //On essaye d'extraire de nouveau la note de la BDD toujours grâce à son nouveau titre projetFromBdd = maprojetDB.getprojetWithNom("J'ai modifié le nom du projete"); //Si aucun livre n'est retourné if(projetFromBdd == null){ //On affiche un message indiquant que la note n'existe pas dans la BDD Toast.makeText(this, "Ce projet n'existe pas dans la BDD", Toast.LENGTH_LONG).show(); } //Si la note existe (mais normalement il ne devrait pas) else{ //on affiche un message indiquant que la note existe dans la BDD Toast.makeText(this, "Ce projet existe dans la BDD", Toast.LENGTH_LONG).show(); } maprojetDB.close(); } }
aussi dans le logcat il m'apparait le suivant:
08-02 09:38:53.666 551-551/formationrs.rrapp D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
08-02 09:38:54.896 551-557/formationrs.rrapp I/dalvikvm﹕ threadid=3: reacting to signal 3
08-02 09:38:55.006 551-557/formationrs.rrapp I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
08-02 09:38:55.376 551-551/formationrs.rrapp D/﹕ success
08-02 09:38:55.406 551-557/formationrs.rrapp I/dalvikvm﹕ threadid=3: reacting to signal 3
08-02 09:38:55.416 551-557/formationrs.rrapp I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
08-02 09:38:55.806 551-551/formationrs.rrapp D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
08-02 09:39:10.706 583-589/formationrs.rrapp I/dalvikvm﹕ threadid=3: reacting to signal 3
08-02 09:39:10.846 583-589/formationrs.rrapp I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
priere de m'indiquer l'erreur SVP![]()
Partager