Aller au contenu

sqlite erreur autoincrement


Recommended Posts

Bonjour, j'essai, depuis quelque jour, d'utiliser sqlite. je programme sur eclipse avec l emulateur.

Voila , j' ai une erreur que je ne comprend pas: autoincrement is only allowed for Integer primary key

Voici mon code:

public class DatabaseHelper  extends SQLiteOpenHelper
{

/**
 * Nom de la DB (nom du fichier SQLite).
 */
private static String DATABASE_NAME = "accesdbtests.db";

/**
 * Numéro de version de la DB.
 * Si ce numéro change, la fonction onUpgrade est exécutée
 * pour effacer le contenu de la DB et recréer la nouvelle
 * version .
 */
private static final int DATABASE_VERSION = 1;

/**
 * Lien vers la session DB.
 */
private SQLiteDatabase db;



  /**
   * Nom de la table de la BD
   */
  private static final String TABLE_PROGRAMMES = "Programmes";

  /**
   * Script SQL de création de la table Programmes.
   */
 private static final String COLONNE_ID = "id";
 private static final int COLONNE_ID_ID = 0;
 private static final String COLONNE_EXO = "exercice";
 private static final int COLONNE_EXO_ID = 1;
 private static final String COLONNE_DUREE = "duree";
 private static final int COLONNE_DUREE_ID = 2;

 private static final String REQUETTE_CREATION_BD = "create table "
   + TABLE_PROGRAMMES + "(" + COLONNE_ID
   + "integer primary key autoincrement," + COLONNE_EXO
   + "text not null, " + COLONNE_DUREE + "text not null)";


 public DatabaseHelper(Context context)
 {
	  super(context, DATABASE_NAME, null, DATABASE_VERSION);
	  db = getWritableDatabase();
 }

 /**
   * Exécuté si la DB n'existe pas.
   */
  @Override
  public void onCreate(SQLiteDatabase db)
  {
	  db.execSQL(REQUETTE_CREATION_BD);
  }

  /**
   * Exécuté chaque fois que le numéro de version de DB change.
   */
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	  db.execSQL("DROP TABLE " + TABLE_PROGRAMMES + ";");
	  onCreate(db);
  }

  /**
   * Insère un exercice dans la BDD
   */
  public long insertExo(Effort effort) {
	  ContentValues exoToInsert = new ContentValues();
	  exoToInsert.put(COLONNE_EXO, effort.getExercice());
	  exoToInsert.put(COLONNE_DUREE, effort.getDuree());
	  return db.insert(TABLE_PROGRAMMES, null, exoToInsert);
  }



  /**
   * Charge le contenu de la table Programmes
   * et retourne ce contenu sous la forme d'une liste de Exercice + Duree ;
   */



  public ArrayList<String> getprogramme() {
	  ArrayList<String> output = new ArrayList<String>();

	  String[] colonnesARecup = new String[] { COLONNE_EXO, COLONNE_DUREE };

	  Cursor cursorResults = db.query(TABLE_PROGRAMMES, colonnesARecup, null,
			  null, null, "exercice asc, duree asc", null);
	  if (null != cursorResults) {
		  if (cursorResults.moveToFirst()) {
			  do {

				  String exerciceduree = cursorResults.getString(COLONNE_EXO_ID)
						  + " " + cursorResults.getString(COLONNE_DUREE_ID);
				  output.add(exerciceduree);
			  } while (cursorResults.moveToNext());
		  } // end§if
	  }

	  return output;
  }
}

Un petit coup de main serai le bien venu, merci

Lien vers le commentaire
Partager sur d’autres sites

Merci de ta reponse je vient donc de modifier cette ligne:

private static final String COLONNE_ID = "_id";

Mais j'ai toujours les memes erreurs:

03-07 09:58:17.207: E/Database(202): Failure 1 (AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY) on 0x118980 when preparing 'create table Programmes(_idinteger primary key autoincrement,exercicetext not null, dureetext not null)'.

03-07 09:58:17.214: D/AndroidRuntime(202): Shutting down VM

03-07 09:58:17.214: W/dalvikvm(202): threadid=3: thread exiting with uncaught exception (group=0x4001b188)

03-07 09:58:17.214: E/AndroidRuntime(202): Uncaught handler: thread main exiting due to uncaught exception

03-07 09:58:17.224: E/AndroidRuntime(202): android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY: create table Programmes(_idinteger primary key autoincrement,exercicetext not null, dureetext not null)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1610)

03-07 09:58:17.224: E/AndroidRuntime(202): at sdz.home_training.DatabaseHelper.onCreate(DatabaseHelper.java:68)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106)

03-07 09:58:17.224: E/AndroidRuntime(202): at sdz.home_training.DatabaseHelper.<init>(DatabaseHelper.java:59)

03-07 09:58:17.224: E/AndroidRuntime(202): at sdz.home_training.BtnCreerOnClick.onClick(BtnCreerOnClick.java:26)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.view.View.performClick(View.java:2364)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.view.View.onTouchEvent(View.java:4179)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.widget.TextView.onTouchEvent(TextView.java:6541)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.view.View.dispatchTouchEvent(View.java:3709)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

03-07 09:58:17.224: E/AndroidRuntime(202): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)

03-07 09:58:17.224: E/AndroidRuntime(202): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)

03-07 09:58:17.224: E/AndroidRuntime(202): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.os.Handler.dispatchMessage(Handler.java:99)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.os.Looper.loop(Looper.java:123)

03-07 09:58:17.224: E/AndroidRuntime(202): at android.app.ActivityThread.main(ActivityThread.java:4363)

03-07 09:58:17.224: E/AndroidRuntime(202): at java.lang.reflect.Method.invokeNative(Native Method)

03-07 09:58:17.224: E/AndroidRuntime(202): at java.lang.reflect.Method.invoke(Method.java:521)

03-07 09:58:17.224: E/AndroidRuntime(202): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

03-07 09:58:17.224: E/AndroidRuntime(202): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

03-07 09:58:17.224: E/AndroidRuntime(202): at dalvik.system.NativeStart.main(Native Method)

03-07 09:58:17.244: I/dalvikvm(202): threadid=7: reacting to signal 3

03-07 09:58:17.244: E/dalvikvm(202): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

Lien vers le commentaire
Partager sur d’autres sites

Il manque juste un espace. L'erreur générée est la suivante

create table Programmes(_idinteger primary key autoincrement,exercicetext not null, dureetext not null

Ajoute un espace avant integer primary key

"create table "

+ TABLE_PROGRAMMES + "(" + COLONNE_ID

+ " integer primary key autoincrement," + COLONNE_EXO

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...