Bonjour,

J'ai voullu faire marcher un exemple d'un livre sur le fonctionnement de la base de données SQLite sur Android. Je ne comprends pas pourquoi mon Main ne fonctionne pas. Ce que je voudrais faire, c'est créer la base de données étant donnée qu'elle n'existe pas en essayant de l'ouvrir, puis la refermer. Mais je ne comprends pas pourquoi l'application plante.

Je vous remercie d'avance

Voilà la classe 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
26
27
 
package com.eyrolles.android.notepad;
 
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
 
public class Main extends Activity {
	PlaneteDBAdaptateur pdba;
	Planete pl1;
	SQLiteDatabase bd;	
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
 
		pl1=new Planete("Mars",1432);
		pdba=new PlaneteDBAdaptateur(this);
		bd=pdba.open();
                bd.close();
	}
}
J'ai une classe Planete:

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
 
package com.eyrolles.android.notepad;
 
public class Planete {
	private int id;
	private String nom;
	private float rayon;
 
	public Planete() { }
 
	public Planete(String nom, float rayon)
	{
		this.nom = nom;
		this.rayon = rayon;
	}
 
	public int getId() {
		return id;
	}
 
	public void setId(int id) {
		this.id = id;
	}
 
	public String getNom() {
		return nom;
	}
 
	public void setNom(String nom) {
		this.nom = nom;
	}
 
	public float getRayon() {
			  return rayon;
	}
 
	public void setRayon(float rayon) {
		this.rayon = rayon;
	}
}
Une classe PlaneteDBAdaptateur qui contient une classe MaBaseOpenHelper:

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
 
package com.eyrolles.android.notepad;
 
import java.util.ArrayList;
 
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
 
public class PlaneteDBAdaptateur {
	private static final int BASE_VERSION=1;
	private static final String BASE_NOM="planetes.db";
 
	private static final String TABLE_PLANETES="table_planetes";
 
	private static final String COLONNE_ID="id";
	private static final int COLONNE_ID_ID=0;
	private static final String COLONNE_NOM="nom";
	private static final int COLONNE_NOM_ID=1;
	private static final String COLONNE_RAYON="rayon";
	private static final int COLONNE_RAYON_ID=2;
 
	private static final String REQUETE_CREATION_BD="create table"
		+ TABLE_PLANETES+" ("+ COLONNE_ID
		+" integer primary key autoincrement, "+ COLONNE_NOM
		+" text not null, " +COLONNE_RAYON+" text not null);";
 
	private SQLiteDatabase maBaseDonnees;
 
	private MaBaseOpenHelper baseHelper;
 
	public PlaneteDBAdaptateur(Context ctx){
		baseHelper=new MaBaseOpenHelper(ctx, BASE_NOM,null,BASE_VERSION);
	}
 
	public SQLiteDatabase open(){
		maBaseDonnees=baseHelper.getReadableDatabase();
		return maBaseDonnees;
	}
 
	public void close(){
		maBaseDonnees.close();
	}
 
	public SQLiteDatabase getBaseDonnnees(){
		return maBaseDonnees;
	}
 
	public Planete getPlanete(String nom){
		Cursor c = maBaseDonnees.query(TABLE_PLANETES, new String[] {
		COLONNE_ID, COLONNE_NOM, COLONNE_RAYON }, null, null, null,
		COLONNE_NOM + " LIKE " + nom, null);
		return cursorToPlanete(c);
 
	}
 
	public Planete getPlanete(int id){
		Cursor c = maBaseDonnees.query(TABLE_PLANETES, new String[] {
		COLONNE_ID, COLONNE_NOM, COLONNE_RAYON }, null, null, null,
		COLONNE_ID + " = " + id, null);
		return cursorToPlanete(c);
 
	}
 
	public Planete cursorToPlanete(Cursor c) {
		// Si la requête ne renvoie pas de résultat.
		if (c.getCount() == 0)
		return null;
		Planete retPlanete = new Planete();
		// Extraction des valeurs depuis le curseur.
		retPlanete.setId(c.getInt(COLONNE_ID_ID));
		retPlanete.setNom(c.getString(COLONNE_NOM_ID));
		retPlanete.setRayon(c.getFloat(COLONNE_RAYON_ID));
		// Ferme le curseur pour libérer les ressources.
		c.close();
		return retPlanete;
		}
 
 
	private ArrayList<Planete> cursorToPlanetes(Cursor c) {
		// Si la requête ne renvoie pas de résultat.
		if (c.getCount() == 0)
		return new ArrayList<Planete>(0);
		ArrayList<Planete> retPlanetes = new ArrayList<Planete>(c.getCount());
		c.moveToFirst();
		do {
		Planete planete = new Planete();
		planete.setId(c.getInt(COLONNE_ID_ID));
		planete.setNom(c.getString(COLONNE_NOM_ID));
		planete.setRayon(c.getFloat(COLONNE_RAYON_ID));
		retPlanetes.add(planete);
		} while (c.moveToNext());
		// Ferme le curseur pour libérer les ressources.
		c.close();
		return retPlanetes;
	}
 
	public long insertPlanete(Planete planete){
		ContentValues valeurs = new ContentValues();
		valeurs.put(COLONNE_NOM, planete.getNom());
		valeurs.put(COLONNE_RAYON, planete.getRayon());
		return maBaseDonnees.insert(TABLE_PLANETES, null, valeurs);
	}
 
	public int updatePlanete(int id, Planete planeteToUpdate){
		ContentValues valeurs = new ContentValues();
		valeurs.put(COLONNE_NOM, planeteToUpdate.getNom());
		valeurs.put(COLONNE_RAYON, planeteToUpdate.getRayon());
		return maBaseDonnees.update(TABLE_PLANETES, valeurs, COLONNE_ID + " = "
		+ id, null);
	}
 
	public int removePlanete(String nom){
		return maBaseDonnees.delete(TABLE_PLANETES, COLONNE_NOM + " LIKE "
				+ nom, null);
	}
 
	public int removePlanete(int id){
		return maBaseDonnees.delete(TABLE_PLANETES, COLONNE_ID + " = " + id,
				null);
	}
 
	private class MaBaseOpenHelper extends SQLiteOpenHelper{
		private static final String TABLE_PLANETES="table_planetes";
 
		private static final String COLONNE_ID="id";
		private static final String COLONNE_NOM="nom";
		private static final String COLONNE_RAYON="rayon";
 
		private static final String REQUETE_CREATION_BD="create table"
			+ TABLE_PLANETES+" ("+ COLONNE_ID
			+" integer primary key autoincrement, "+ COLONNE_NOM
			+" text not null, " +COLONNE_RAYON+" text not null);";
 
		public MaBaseOpenHelper(Context context, String nom, CursorFactory cursorfactory, int version){
			super(context, nom, cursorfactory, version);
		}
 
 
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(REQUETE_CREATION_BD);
		}
 
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE "+ TABLE_PLANETES+ ";");
			onCreate(db);
		}
	}
}