Aller au contenu

Erreur de contrainte incompréhensible!


Recommended Posts

Salut les jeunes!

j'y suis depuis ce matin et je ne trouve pas le problème!

Voilà la classe incriminée :

package com.swir.android.data;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DataDBAdaptor {
   private static final String BASE_NAME = "swirdb.db";
   private static final int BASE_VERSION = 1;

   private DatabaseOpenHelper baseHelper;
   private SQLiteDatabase database;

   public DataDBAdaptor(Context context) {
       baseHelper = new DatabaseOpenHelper(context, BASE_NAME, null,
               BASE_VERSION);
   }

   public SQLiteDatabase open() {
       database = baseHelper.getWritableDatabase();
       return database;
   }

   public void close() {
       database.close();
   }

   public List getAssets() {
       Cursor cursorAssets = database.query(DatabaseOpenHelper.TABLE_ASSETS,
               null, null, null, null, null, null);
       List result = new ArrayList();
       if (cursorAssets.moveToFirst()) {
           do {
               String assetId = cursorAssets.getString(0);
               String deviceId = cursorAssets.getString(1);
               Asset currentAsset = new Asset(deviceId, assetId);
               result.add(currentAsset);
               Cursor cursorVar = database.query(
                       DatabaseOpenHelper.TABLE_VARIABLES, new String[] {
                               DatabaseOpenHelper.COL_VAR_NAME,
                               DatabaseOpenHelper.COL_VAR_PATH,
                               DatabaseOpenHelper.COL_VAR_TYPE },
                       DatabaseOpenHelper.COL_ASSET_ID + " = " + assetId
                               + " AND " + DatabaseOpenHelper.COL_DEVICE_ID
                               + " = " + deviceId, null, null, null, null);
               if (cursorVar.moveToFirst()) {
                   do {
                       String name = cursorVar.getString(0);
                       String path = cursorVar.getString(1);
                       int type = cursorVar.getInt(2);
                       new Variable(path, name, type, currentAsset);
                   } while (cursorVar.moveToNext());
               }
               cursorVar.close();
           } while (cursorAssets.moveToNext());
       }
       cursorAssets.close();
       return null;
   }

   public long insertAsset(Asset asset) {
       ContentValues values = new ContentValues();
       values.put(DatabaseOpenHelper.COL_ASSET_ID, asset.getId());
       values.put(DatabaseOpenHelper.COL_DEVICE_ID, asset.getDeviceUniqueId());
       return database.insert(DatabaseOpenHelper.TABLE_ASSETS, null, values);
   }

   public long insertVariable(Variable variable) {
       ContentValues values = new ContentValues();
       values
               .put(DatabaseOpenHelper.COL_ASSET_ID, variable.getAsset()
                       .getId());
       values.put(DatabaseOpenHelper.COL_DEVICE_ID, variable.getAsset()
               .getDeviceUniqueId());
       values.put(DatabaseOpenHelper.COL_VAR_NAME, variable.getName());
       values.put(DatabaseOpenHelper.COL_VAR_PATH, variable.getPath());
       values.put(DatabaseOpenHelper.COL_VAR_TYPE, variable.getType());
       return database
               .insert(DatabaseOpenHelper.TABLE_VARIABLES, null, values);
   }

   public int removeVariable(Variable variable) {
       String path = DatabaseOpenHelper.COL_VAR_PATH + " = "
               + variable.getPath();
       String name = DatabaseOpenHelper.COL_VAR_NAME + " = "
               + variable.getName();
       String assetId = DatabaseOpenHelper.COL_ASSET_ID + " = "
               + variable.getAsset().getId();
       String deviceId = DatabaseOpenHelper.COL_DEVICE_ID + " = "
               + variable.getAsset().getDeviceUniqueId();
       String where = path + " AND " + name + " AND " + assetId + " AND "
               + deviceId;
       return database.delete(DatabaseOpenHelper.TABLE_VARIABLES, where, null);
   }

   public int removeAsset(Asset asset) {
       for (int i = 0; i < asset.getVariableSize(); i++) {
           removeVariable(asset.getVariableWithValueAt(i));
       }
       String assetId = DatabaseOpenHelper.COL_ASSET_ID + " = "
               + asset.getId();
       String deviceId = DatabaseOpenHelper.COL_DEVICE_ID + " = "
               + asset.getDeviceUniqueId();
       String where = assetId + " AND " + deviceId;
       return database.delete(DatabaseOpenHelper.TABLE_ASSETS, where, null);
   }

   public void clean() {
       baseHelper.clean(database);
   }
}

Et l'erreur reproduit à chaque insertion d'une variable bien sur!

Error inserting var_type=0 asset_id=IO var_name=avg device_id=DAX1 var_path=/ANALOG1
android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
   at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
   at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:66)
   at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1313)
   at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1173)
   at com.swir.android.data.DataDBAdaptor.insertVariable(DataDBAdaptor.java:83)
   at com.swir.android.data.DataManager.updateAllVariables(DataManager.java:426)
   at com.swir.android.data.DataManager.initialisation(DataManager.java:151)
   at com.swir.android.data.DataManager.getInstance(DataManager.java:93)
   at com.swir.android.activities.MainActivity.onCreate(MainActivity.java:90)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
   at android.app.ActivityThread.access$2100(ActivityThread.java:116)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:4203)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
   at dalvik.system.NativeStart.main(Native Method)

Quelqu'un voit une erreur de contraintes ??? ou y a-t-il un moyen de dumper la requète SQL envoyée ?

Sachant que j'ai fait tout à la main avec sqlite3 et ca n'a pas merdé!!! Je ne comprends pas!

Lien vers le commentaire
Partager sur d’autres sites

La classe :

package com.swir.android.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DatabaseOpenHelper extends SQLiteOpenHelper {
   /* Informations for table ASSETS */
   public static final String TABLE_ASSETS = "Asset";
   public static final String COL_ASSET_ID = "asset_id";
   public static final String COL_DEVICE_ID = "device_id";

   private static final String CREATE_TABLE_ASSET = "CREATE TABLE "
           + TABLE_ASSETS + "( " + COL_ASSET_ID + " TEXT NOT NULL,"
           + COL_DEVICE_ID + " TEXT NOT NULL,"
           + " CONSTRAINT PK_ASSET PRIMARY KEY (" + COL_ASSET_ID + ", "
           + COL_DEVICE_ID + ")" + " );";

   /* Informations for table VARIABLES */
   public static final String TABLE_VARIABLES = "Variable";
   public static final String COL_VAR_PATH = "var_path";
   public static final String COL_VAR_NAME = "var_name";
   public static final String COL_VAR_TYPE = "var_type";
   public static final String COL_VAR_EXP = "var_expanded";

   private static final String CREATE_TABLE_VARIABLE = "CREATE TABLE "
           + TABLE_VARIABLES + "( " + COL_VAR_PATH + " TEXT NOT NULL, "
           + COL_VAR_NAME + " TEXT NOT NULL, " + COL_VAR_TYPE
           + " INTEGER NOT NULL ," + COL_VAR_EXP + " INTEGER NOT NULL,"
           + COL_ASSET_ID + " TEXT NOT NULL, " + COL_DEVICE_ID
           + " TEXT NOT NULL, " + " CONSTRAINT PK_ASSET PRIMARY KEY ("
           + COL_VAR_NAME + "," + COL_VAR_PATH + "," + COL_ASSET_ID + ", "
           + COL_DEVICE_ID + ")" + ");";

   public DatabaseOpenHelper(Context context, String name,
           CursorFactory factory, int version) {
       super(context, name, factory, version);
       // TODO Auto-generated constructor stub
   }

   @Override
   public void onCreate(SQLiteDatabase db) {
       db.execSQL(CREATE_TABLE_ASSET);
       db.execSQL(CREATE_TABLE_VARIABLE);
   }

   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       clean(db);
   }

   public void clean(SQLiteDatabase db) {
       db.execSQL("DELETE FROM " + TABLE_ASSETS + ";");
       db.execSQL("DELETE FROM " + TABLE_VARIABLES + ";");
   }

}

Et le code exécuté en lisible :

CREATE TABLE Asset( asset_id TEXT NOT NULL,device_id TEXT NOT NULL, CONSTRAINT PK_ASSET PRIMARY KEY (asset_id, device_id) );
CREATE TABLE Variable( var_path TEXT NOT NULL, var_name TEXT NOT NULL, var_type INTEGER NOT NULL ,var_expanded INTEGER NOT NULL,asset_id TEXT NOT NULL, device_id TEXT NOT NULL,  CONSTRAINT PK_ASSET PRIMARY KEY (var_name,var_path,asset_id, device_id));

Lien vers le commentaire
Partager sur d’autres sites

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

×
×
  • Créer...