Base de données : changement de version
Bonjour,
Je suis entrain de manipuler une base de donnée, la base se crée bien pour la version 1 mais quand je change de version pour avoir deux bases de données j'ai un message d'erreur
AndroidTest
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
public class Test extends AndroidTestCase {
String tag = "TEST_BDD";
public void testCreation()
{
//Tache_BDD t_bdd = new Tache_BDD(this.getContext(),"BDD_TEST",null,1);
Tache_BDD t_bdd = new Tache_BDD(this.getContext(),"BDD_TEST",null,2);
Log.i(tag, "liste des tables " + t_bdd.allTableNames());
}
} |
Code:
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
|
public class Tache_BDD extends SQLiteOpenHelper {
String tag = "Tache_DBB";
private static final String TABLE_TACHE = "table_tache";
private static final String COL_LIGNE = "LIGNE";
private static final String COL_TITRE = "TITRE";
private static final String COL_PRIORITE = "PRIORITE";
private static final String CREATE_BDD = "CREATE TABLE " + TABLE_TACHE + " ("
+ COL_LIGNE + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_TITRE + " TEXT NOT NULL, "
+ COL_PRIORITE + " INTEGER);";
public Tache_BDD(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
{
super(context,name,factory,version);
Log.i(tag, "Constructeur");
}
@Override
public void onCreate (SQLiteDatabase db)
{
Log.i(tag, "Création");
db.execSQL(CREATE_BDD);
Log.i(tag, "Fin de création");
}
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.i(tag, "mise à jour");
db.execSQL("DROP TABLE IF EXIST "+ TABLE_TACHE);
onCreate(db);
Log.i(tag, "Fin de la mise à jour");
}
public List<String> allTableNames() {
List<String> result = new ArrayList<String>() ;
String selectQuery = "select name from sqlite_master where type = 'table'" ;
Cursor c = this.getReadableDatabase().rawQuery(selectQuery, null);
if (c.moveToFirst()) {
do {
String n = c.getString(c.getColumnIndex("name"));
result.add(n);
} while (c.moveToNext());
}
return result;
}
} |
Message d'erreur
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
Test running started
android.database.sqlite.SQLiteException: near "EXIST": syntax error (code 1): , while compiling: DROP TABLE IF EXIST table_tache
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
at com.example.utilisateur.basededonnee.Tache_BDD.onUpgrade(Tache_BDD.java:54)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:257)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
at com.example.utilisateur.basededonnee.Tache_BDD.allTableNames(Tache_BDD.java:62)
at com.example.utilisateur.basededonnee.Test.testCreation(Test.java:17)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
Finish |