Bonjour à tous,
j'ai un soucis avec ma base SQLite. Je tente, sans succès, d'insérer des données dans cette base.
Voici le code :
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 ObservationSQLite { private String nom; private String espece; private String description; private String image; private String latitude; private String longitude; public ObservationSQLite(){} public ObservationSQLite(String nom, String espece, String description, String image, String latitude, String longitude){ this.nom = nom; this.espece = espece; this.description = description; this.image = image; this.latitude = latitude; this.longitude = longitude; } public String getNom(){ return nom; } public void setNom (String val){ this.nom = val; } public String getEspece(){ return espece; } public void setEspece (String val){ this.espece = val; } public String getDescription(){ return description; } public void setDescription(String val){ this.description = val; } public String getImage(){ return image; } public void setImage(String val){ this.image = val; } public String getLatitude(){ return latitude; } public void setLatitude(String val){ this.latitude = val; } public String getLongitude(){ return longitude; } public void setLongitude(String val){ this.longitude = val; } }
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 public class BaseSQLite extends SQLiteOpenHelper { private static final String TABLE_OBSERVATIONS = "table_observations"; private static final String COL_NOM = "NOM"; private static final String COL_ESPECE = "ESPECE"; private static final String COL_DESCRIPTION = "DESCRIPTION"; private static final String COL_IMAGE = "IMAGE"; private static final String COL_LATITUDE = "LATITUDE"; private static final String COL_LONGITUDE = "LONGITUDE"; private static final String CREATE_BDD = "CREATE TABLE " + TABLE_OBSERVATIONS + " (" + COL_NOM + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_ESPECE + " TEXT NOT NULL, " + COL_DESCRIPTION + " TEXT NOT NULL, " + COL_IMAGE + " TEXT NOT NULL, " + COL_LATITUDE + " TEXT NOT NULL, " + COL_LONGITUDE + " TEXT NOT NULL);"; public BaseSQLite(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { //on créé 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 fait 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 db.execSQL("DROP TABLE " + TABLE_OBSERVATIONS + ";"); onCreate(db); } }Et la partie concernant l'insert :
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 class ObservationsBDD { private static final int VERSION_BDD = 1; private static final String NOM_BDD = "observations.db"; private static final String TABLE_OBSERVATIONS = "table_observations"; private static final String COL_NOM = "NOM"; private static final String COL_ESPECE = "ESPECE"; private static final String COL_DESCRIPTION = "DESCRIPTION"; private static final String COL_IMAGE = "IMAGE"; private static final String COL_LATITUDE = "LATITUDE"; private static final String COL_LONGITUDE = "LONGITUDE"; private SQLiteDatabase bdd; private BaseSQLite maBaseSQLite; public ObservationsBDD(Context context){ //On créer la BDD et sa table maBaseSQLite = new BaseSQLite(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 insertObservation(ObservationSQLite obs){ //Création d'un ContentValues (fonctionne comme une HashMap) ContentValues values = new ContentValues(); //on lui ajoute une valeur associé à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur) values.put(COL_NOM, obs.getNom()); values.put(COL_ESPECE, obs.getEspece()); values.put(COL_DESCRIPTION, obs.getDescription()); values.put(COL_IMAGE, obs.getImage()); values.put(COL_LATITUDE, obs.getLatitude()); values.put(COL_LONGITUDE, obs.getLongitude()); //on insère l'objet dans la BDD via le ContentValues return bdd.insert(TABLE_OBSERVATIONS, null, values); } public ObservationSQLite getObs (String nom){ //Récupère dans un Cursor les valeur correspondant à un livre contenu dans la BDD (ici on sélectionne le livre grâce à son titre) Cursor c = bdd.query(TABLE_OBSERVATIONS, new String[] {COL_NOM, COL_ESPECE, COL_DESCRIPTION, COL_IMAGE, COL_LATITUDE, COL_LONGITUDE}, COL_NOM + " LIKE \"" + nom +"\"", null, null, null, null); return cursorToObs(c); } private ObservationSQLite cursorToObs(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 ObservationSQLite obs = new ObservationSQLite(); //on lui affecte toutes les infos grâce aux infos contenues dans le Cursor obs.setNom(c.getString(0)); obs.setEspece(c.getString(1)); obs.setDescription(c.getString(2)); obs.setImage(c.getString(3)); obs.setLatitude(c.getString(4)); obs.setLongitude(c.getString(5)); //On ferme le cursor c.close(); //On retourne l'observation return obs; } }
Au moment de l'insert, j'obtiens donc l'erreur :
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 BretourCr = (Button) findViewById(R.id.B_retourCr); BvaliderCr = (Button) findViewById(R.id.B_validerCr); nom = (EditText) findViewById(R.id.saisie_nom); espece= (EditText) findViewById(R.id.saisie_espece); description= (EditText) findViewById(R.id.saisie_description); image= (EditText) findViewById(R.id.saisie_image); ObservationsBDD obs = new ObservationsBDD(this); ObservationSQLite observation = new ObservationSQLite(nom.getText().toString(), espece.getText().toString(), description.getText().toString(), image.getText().toString(), "aaa" , "bbb"); obs.open(); Toast.makeText(this, "Ouverture ok", Toast.LENGTH_SHORT).show(); Toast.makeText(this,nom.getText().toString() + " " + espece.getText().toString() + " " + description.getText().toString() + " " + image.getText().toString() , Toast.LENGTH_LONG).show(); obs.insertObservation(observation); obs.close(); Toast.makeText(this, "Fermeture ok", Toast.LENGTH_SHORT).show();
J'ai bien les toast qui s'affichent pour me dire Ouverture OK et fermeture OK donc ce n'est pas une erreur bloquante même si l'insertion ne se fait pas.06-02 10:21:45.515: E/SQLiteDatabase(677): Error inserting NOM=essai IMAGE=fnnfnf LONGITUDE=bbb ESPECE=test DESCRIPTION=essaiTest LATITUDE=aaa
06-02 10:21:45.515: E/SQLiteDatabase(677): android.database.sqlite.SQLiteDatatypeMismatchException: error code 20: datatype mismatch
J'ai rien trouvé de probant sur le net par rapport à cette erreur, de plus, comme j'ai uniquement des string, pour le moment, je ne comprends pas trop trop.
Je suis quasi sur que c'est une faute "con" mais je ne vois pas laquelle.
Merci de votre aide.
Partager