Bonjour !
J'ai un petit soucis qui me tracasse, je fais une application android où je dois utiliser une base de données avec 2 tables.
J'utilise une première table dans ma vue principale, puis une autre dans une vue secondaire (accessible depuis la vue principale via un simple bouton).
La première table ne pose aucun problème, je peux y lire et écrire sans problème, le soucis c'est que quand j'accède à ma seconde vue, pour lire dans la seconde table j'ai une erreur qui me dit qu'elle n'existe pas.
Voici mes classes qui gèrent la base :
Le code pour accéder à la table player, qui fonctionne :
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 public class DBAdapter extends SQLiteOpenHelper { protected static final String DATABASE_FILE = "base.db"; protected static final int VERSION = 1; public DBAdapter(Context context) { super(context, DATABASE_FILE, null, VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(PlayerTable.CREATE_TABLE); db.execSQL(GameTable.CREATE_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS "+PlayerTable.CREATE_TABLE+";"); db.execSQL("DROP TABLE IF EXISTS "+GameTable.CREATE_TABLE+";"); onCreate(db); } public SQLiteDatabase getDatabase() { return getWritableDatabase(); } }
Le code pour accéder à la table game, j'ai raccourci la méthode getGames qui pose problème au niveau de l'instruction database.rawQuery :
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 public class PlayerTable { public static final String TABLE_NAME = "player"; public static final String FIELD_KEY = "idPlayer"; public static final String FIELD_NAME = "name"; public static final String FIELD_SEX = "sex"; protected static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+" (" + FIELD_KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, " + FIELD_NAME + " TEXT NOT NULL, " + FIELD_SEX + " INTEGER NOT NULL DEFAULT 0" + ");"; private SQLiteDatabase database; public PlayerTable(Context context) { DBAdapter tmp = new DBAdapter(context); database = tmp.getDatabase(); } public void addPlayer(String name, boolean sex) { int sex2 = sex == true ? 1 : 0; String query = "INSERT INTO "+TABLE_NAME+" ("+ FIELD_NAME+", "+ FIELD_SEX+ ") VALUES('"+name+"', "+sex2+");"; database.execSQL(query); } public String [] getPlayers() { String query = "SELECT "+FIELD_NAME+" FROM "+TABLE_NAME+";"; Cursor cursor = database.rawQuery(query, null); if(cursor.moveToFirst()) { String [] listPlayers = new String[cursor.getCount()]; do { listPlayers[cursor.getPosition()] = cursor.getString(0); } while(cursor.moveToNext()); return listPlayers; } return null; } }
Et le message d'erreur assez explicite :
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 public class GameTable { public static final String TABLE_NAME = "game"; public static final String FIELD_KEY = "idGame"; public static final String FIELD_CAT = "category"; public static final String FIELD_NAME = "name"; public static final String FIELD_DESC = "description"; public static final String FIELD_NBPLAYER = "nbPlayer"; public static final String FIELD_ENABLED = "enabled"; protected static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+" (" + FIELD_KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, " + FIELD_CAT + " INTEGER NOT NULL DEFAULT 0, " + FIELD_NAME + " TEXT NOT NULL, " + FIELD_DESC + " TEXT NOT NULL, " + FIELD_NBPLAYER + " INTEGER NOT NULL DEFAULT 2, " + FIELD_ENABLED + " INTEGER NOT NULL DEFAULT 0" + ");"; private SQLiteDatabase database; public GameTable(Context context) { DBAdapter tmp = new DBAdapter(context); database = tmp.getDatabase(); } public List<Game> getGames() { String query = "SELECT * FROM "+TABLE_NAME+";"; Cursor cursor = database.rawQuery(query, null); return null; }
Voilà bon ça fait beaucoup pour pas grand chose, mais je ne veux pas omettre de détail.
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 09-17 17:41:17.349: I/Database(476): sqlite returned: error code = 1, msg = no such table: game 09-17 17:41:17.349: D/AndroidRuntime(476): Shutting down VM 09-17 17:41:17.349: W/dalvikvm(476): threadid=1: thread exiting with uncaught exception (group=0x40015560) 09-17 17:41:17.358: E/AndroidRuntime(476): FATAL EXCEPTION: main 09-17 17:41:17.358: E/AndroidRuntime(476): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.view/app.view.Settings}: android.database.sqlite.SQLiteException: no such table: game: , while compiling: SELECT * FROM game; 09-17 17:41:17.358: E/AndroidRuntime(476): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 09-17 17:41:17.358: E/AndroidRuntime(476): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 09-17 17:41:17.358: E/AndroidRuntime(476): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 09-17 17:41:17.358: E/AndroidRuntime(476): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 09-17 17:41:17.358: E/AndroidRuntime(476): at android.os.Handler.dispatchMessage(Handler.java:99) 09-17 17:41:17.358: E/AndroidRuntime(476): at android.os.Looper.loop(Looper.java:130) 09-17 17:41:17.358: E/AndroidRuntime(476): at android.app.ActivityThread.main(ActivityThread.java:3683) 09-17 17:41:17.358: E/AndroidRuntime(476): at java.lang.reflect.Method.invokeNative(Native Method) 09-17 17:41:17.358: E/AndroidRuntime(476): at java.lang.reflect.Method.invoke(Method.java:507) 09-17 17:41:17.358: E/AndroidRuntime(476): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 09-17 17:41:17.358: E/AndroidRuntime(476): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 09-17 17:41:17.358: E/AndroidRuntime(476): at dalvik.system.NativeStart.main(Native Method) 09-17 17:41:17.358: E/AndroidRuntime(476): Caused by: android.database.sqlite.SQLiteException: no such table: game: , while compiling: SELECT * FROM game; 09-17 17:41:17.358: E/AndroidRuntime(476): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 09-17 17:41:17.358: E/AndroidRuntime(476): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92) 09-17 17:41:17.358: E/AndroidRuntime(476): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65) 09-17 17:41:17.358: E/AndroidRuntime(476): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83) 09-17 17:41:17.358: E/AndroidRuntime(476): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49) 09-17 17:41:17.358: E/AndroidRuntime(476): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42) 09-17 17:41:17.358: E/AndroidRuntime(476): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356) 09-17 17:41:17.358: E/AndroidRuntime(476): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324) 09-17 17:41:17.358: E/AndroidRuntime(476): at app.database.GameTable.getGames(GameTable.java:57) 09-17 17:41:17.358: E/AndroidRuntime(476): at app.view.Settings.onCreate(Settings.java:24) 09-17 17:41:17.358: E/AndroidRuntime(476): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 09-17 17:41:17.358: E/AndroidRuntime(476): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 09-17 17:41:17.358: E/AndroidRuntime(476): ... 11 more 09-17 17:41:46.919: I/Process(476): Sending signal. PID: 476 SIG: 9
J'ai recherché un paquet de temps et je ne comprends vraiment pas le soucis. Je me demande si ce n'est pas du au fait que j'instancie 2 fois DBAdapter (étant donné que c'est sur 2 vues différentes).
J'ai tout de même l'impression que gérer plusieurs tables est relativement délicat vu les sujets que j'ai vu dans mes recherches.
J'espère que vous pourrez m'aider, et merci d'avance.
Bonne soirée.
Partager