Bonjour ,

J'ai un soucis avec ma base de donnée , je cherche à faire une base de donnée , contenant deux tables , une table exercice :
– id : entier, auto incrémenté et clé primaire
– name : string, nom de l’exercice
et une table score :
– id_exercice : entier, clé étrangère
– value : entier
– date : date et heure d’obtention du score

Hors lorsque j'applique mon code , j'ai le premier insert pour le nom de l'exercice qui fonctionne bien ainsi que le ID , mais pour le Insert du score ça ne fonctionne pas. Et petite question secondaire , comment je peux récupérer la date est l'heure , et quelle est le type de champ (String , format date ? ) dans la base de donnée pour là stocker.

PS: je ne suis pas sûr de la syntaxe du Insert pour la clé étrangère de la table score ...

Je vous joins les logs + une partie de mon code :

Voici les logs :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
11-20 13:38:14.551: E/SQLiteDatabase(721): Error inserting SCORE=20
11-20 13:38:14.551: E/SQLiteDatabase(721): android.database.sqlite.SQLiteException: no such table: table_score: , while compiling: INSERT INTO table_score(SCORE) VALUES (?)
11-20 13:38:14.551: E/SQLiteDatabase(721): 	at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
11-20 13:38:14.551: E/SQLiteDatabase(721): 	at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
11-20 13:38:14.551: E/SQLiteDatabase(721): 	at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:146)
11-20 13:38:14.551: E/SQLiteDatabase(721): 	at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:367)
11-20 13:38:14.551: E/SQLiteDatabase(721): 	at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:253)
11-20 13:38:14.551: E/SQLiteDatabase(721): 	at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:111)
Voici mon code :

DataBase :

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
package com.example.testbase2;
 
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
 
public class MyDatebase extends SQLiteOpenHelper {
 
	private static final String TABLE_EXERCICE = "table_exercices";
	private static final String COL_ID = "ID";
	private static final String COL_NOM_EXERCICE = "Exercice1";
 
	private static final String TABLE_SCORE = "table_score";
	private static final String COL_ID_SCORE = "ID_SCORE";
	private static final int VALUE = 0;
 
	private static final String CREATE_BDD = "CREATE TABLE " + TABLE_EXERCICE + " ("
	+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+ COL_NOM_EXERCICE + " TEXT NOT NULL);";
 
	private static final String CREATE_BDD2 = "CREATE TABLE " + TABLE_SCORE + " ("
	+ COL_ID_SCORE + " FOREIGN KEY "+ COL_ID +" REFERENCES, "+ TABLE_EXERCICE + VALUE + "TEXT NOT NULL);";
 
	public MyDatebase(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);
		db.execSQL(CREATE_BDD2);
	}
 
	@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
		db.execSQL("DROP TABLE " + TABLE_EXERCICE + ";");
		db.execSQL("DROP TABLE " + TABLE_SCORE + ";");
		onCreate(db);
	}
 
}

ExercicesBDD :

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
package com.example.testbase2;
 
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
 
public class ExercicesBDD {
 
	private static final int VERSION_BDD = 1;
	private static final String NOM_BDD = "exercices1.db";
 
	private static final String TABLE_EXERCICE = "table_exercices";
	private static final String TABLE_SCORE = "table_score";
	private static final int NUM_COL_ID_SCORE = 0;
	private static final String COL_ID_SCORE = "ID_SCORE";
	private static final int NUM_COL_SCORE = 1;
	private static final String COL_NOM_SCORE = "SCORE";
	private static final int VALUE = 0;
 
	private static final String COL_ID = "ID";
	private static final int NUM_COL_ID = 0;
	private static final String COL_NOM_EXERCICE = "Exercice1";
	private static final int NUM_COL_TITRE = 1;
 
	private SQLiteDatabase bdd;
 
	private MyDatebase maBaseSQLite;
 
	public ExercicesBDD(Context context){
		//On crée la BDD et sa table
		maBaseSQLite = new MyDatebase(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 insertExercice(Exercice exercice){
		//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_EXERCICE, exercice.getTitre());
		System.out.println("insertion du nom d'exercice en base");
		//on insère l'objet dans la BDD via le ContentValues
 
		return bdd.insert(TABLE_EXERCICE, null, values);
	}
 
	public long insertExercice2(Exercice exercice){
		ContentValues values2 = new ContentValues();
		values2.put(COL_NOM_SCORE, exercice.getValue());
		System.out.println("insertion du score en base");
		return bdd.insert(TABLE_SCORE, null, values2);
	}
 
	public int updateExercice(int id, Exercice exercice){
		//La mise à jour de l'exercice dans la BDD fonctionne plus ou moins comme une insertion
		//il faut simplement préciser quel exercice on doit mettre à jour grâce à l'ID
		ContentValues values = new ContentValues();
		values.put(COL_NOM_EXERCICE, exercice.getTitre());
		return bdd.update(TABLE_EXERCICE, values, COL_ID + " = " +id, null);
	}
 
	public int removeExerciceWithID(int id){
		//Suppression d'un exercice de la BDD grâce à l'ID
		return bdd.delete(TABLE_EXERCICE, COL_ID + " = " +id, null);
	}
 
	public Exercice getExerciceWithNom(String titre){
		//Récupère dans un Cursor les valeurs correspondant à un exercice contenu dans la BDD (ici on sélectionne l'exercice grâce à son titre)
		Cursor c = bdd.query(TABLE_EXERCICE, new String[] {COL_ID, COL_NOM_EXERCICE}, COL_NOM_EXERCICE + " LIKE \"" + titre +"\"", null, null, null, null);
		return cursorToLivre(c);
	}
 
	//Cette méthode permet de convertir un cursor en un exercice
	private Exercice 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 exercice
		Exercice exercice = new Exercice();
		//on lui affecte toutes les infos grâce aux infos contenues dans le Cursor
		exercice.setId(c.getInt(NUM_COL_ID));
		exercice.setNom(c.getString(NUM_COL_TITRE));
		exercice.setValue(c.getInt(NUM_COL_SCORE));
		//On ferme le cursor
		c.close();
 
		//On retourne l'exercice
		return exercice;
	}
}