IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

[SQLite] Erreur insertion


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2015
    Messages
    39
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2015
    Messages : 39
    Par défaut [SQLite] Erreur insertion
    Bonjour

    J'utilise le tuto d'Axon sur "comment utiliser SQLite sous Android" que j'ai trouvé ici et qui bien sûr marche bien
    Je le modifie pour apprendre le fonctionnement de SQLite
    J'ai simplement ajouté une colonne "date" dont le format importe peu pour l'instant et j'ai une erreur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    SQLiteDatabase: Error inserting Titre=oui ISBN=bonjour Date=267278
    android.database.sqlite.SQLiteException: table table_livres has no column named Date (code 1): , while compiling: INSERT INTO table_livres(Titre,ISBN,Date) VALUES (?,?,?)
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    Voilà les parties en gras que j'ai modifiées:
    (Par contre, je ne sais pas d'où vient la GoogleApi)
    Class MainActivity

    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
    public class MainActivity extends Activity {
        /**
         * ATTENTION: This was auto-generated to implement the App Indexing API.
         * See https://g.co/AppIndexing/AndroidStudio for more information.
         */
        private GoogleApiClient client;
    
        /**
         * Called when the activity is first created.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Calendar c = Calendar.getInstance();
            int seconds = c.get(Calendar.SECOND);
            setContentView(R.layout.activity_main);
            TextView t1 = (TextView) findViewById(R.id.t1);
            t1.setText("x");
           LivresBDD livreBdd = new LivresBDD(this);
    
            Livre livre = new Livre("bonjour", "oui", 267278);
    
             livreBdd.open();
           
            livreBdd.insertLivre(livre);
    
            
            Livre livreFromBdd = livreBdd.getLivreWithTitre(livre.getTitre());
                  if (livreFromBdd != null) {
                     t1.setText(livreFromBdd.toString());
                     livreFromBdd.setTitre("J'ai modifié le titre du livre");
                //j'exclus ça pour l'instant
                //livreBdd.updateLivre(livreFromBdd.getId(), livreFromBdd);
            }
    
           
            livreFromBdd = livreBdd.getLivreWithTitre("J'ai modifié le titre du livre");
          
            if (livreFromBdd != null) {
                Toast.makeText(this, livreFromBdd.toString(), Toast.LENGTH_LONG).show();
                //j'exclus ça pour l'instant
               // livreBdd.removeLivreWithID(livreFromBdd.getId());
            }
    
          
            livreFromBdd = livreBdd.getLivreWithTitre("J'ai modifié le titre du livre");
                   if (livreFromBdd == null) {
                       Toast.makeText(this, "Ce livre n'existe pas dans la BDD", Toast.LENGTH_LONG).show();
            }
                 else {
                            Toast.makeText(this, "Ce livre existe dans la BDD", Toast.LENGTH_LONG).show();
            }
    
            livreBdd.close();
            // ATTENTION: This was auto-generated to implement the App Indexing API.
            // See https://g.co/AppIndexing/AndroidStudio for more information.
            client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
        }
    
        @Override
        public void onStart() {
            super.onStart();
    
            // ATTENTION: This was auto-generated to implement the App Indexing API.
            // See https://g.co/AppIndexing/AndroidStudio for more information.
            client.connect();
            Action viewAction = Action.newAction(
                    Action.TYPE_VIEW, // TODO: choose an action type.
                    "Main Page", // TODO: Define a title for the content shown.
                    // TODO: If you have web page content that matches this app activity's content,
                    // make sure this auto-generated web page URL is correct.
                    // Otherwise, set the URL to null.
                    Uri.parse("http://host/path"),
                    // TODO: Make sure this auto-generated app deep link URI is correct.
                    Uri.parse("android-app://be.psycho_oncologie.livre/http/host/path")
            );
            AppIndex.AppIndexApi.start(client, viewAction);
        }
    
        @Override
        public void onStop() {
            super.onStop();
    
            // ATTENTION: This was auto-generated to implement the App Indexing API.
            // See https://g.co/AppIndexing/AndroidStudio for more information.
            Action viewAction = Action.newAction(
                    Action.TYPE_VIEW, // TODO: choose an action type.
                    "Main Page", // TODO: Define a title for the content shown.
                    // TODO: If you have web page content that matches this app activity's content,
                    // make sure this auto-generated web page URL is correct.
                    // Otherwise, set the URL to null.
                    Uri.parse("http://host/path"),
                    // TODO: Make sure this auto-generated app deep link URI is correct.
                    Uri.parse("android-app://be.psycho_oncologie.livre/http/host/path")
            );
            AppIndex.AppIndexApi.end(client, viewAction);
            client.disconnect();
        }
    Classe Livre

    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
    public class Livre {
    
        private int id;    private String isbn;    private String titre;
        private long date;
    
        public Livre(){}
    
        public Livre(String isbn, String titre, long date){
            this.isbn = isbn;
            this.titre= titre;
            this.date = date;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getIsbn() {
            return isbn;
        }
    
        public void setIsbn(String isbn) {
            this.isbn = isbn;
        }
    
        public String getTitre() {
            return titre;
        }
    
        public void setTitre(String titre) {
            this.titre = titre;
        }
        public long getDate() {
            return date;
        }
        public void setDate(long date) {
            this.date = date;
        }
        public String toString(){
            return "ID : "+id+"\nISBN : "+isbn+"\nDate : "+date;
        }
    Classe LivreBDD
    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
    public class LivresBDD {
        private static final int VERSION_BDD = 1;
        private static final String NOM_BDD = "eleves.db";
        private static final String TABLE_LIVRES = "table_livres";
        private static final String COL_ID = "ID";
        private static final int NUM_COL_ID = 0;
        private static final String COL_ISBN = "ISBN";
        private static final int NUM_COL_ISBN = 1;
        private static final String COL_TITRE = "Titre";
        private static final int NUM_COL_TITRE = 2;
        private static final String COL_DATE = "Date";
        private static final int NUM_COL_DATE = 3;
    
        private SQLiteDatabase bdd;
    
        private MaBaseSQLite maBaseSQLite;
    
        public LivresBDD(Context context){
              maBaseSQLite = new MaBaseSQLite(context, NOM_BDD, null, VERSION_BDD);
        }
    
        public void open(){
            bdd = maBaseSQLite.getWritableDatabase();
        }
    
        public void close(){
             bdd.close();
        }
    
        public SQLiteDatabase getBDD(){
            return bdd;
        }
    
        public long insertLivre(Livre livre){
            ContentValues values = new ContentValues();
            values.put(COL_ISBN, livre.getIsbn());
            values.put(COL_TITRE, livre.getTitre());
            values.put(COL_DATE, livre.getDate());
            return bdd.insert(TABLE_LIVRES, null, values);
        }
    
        public int updateLivre(int id, Livre livre){
            ContentValues values = new ContentValues();
            values.put(COL_ISBN, livre.getIsbn());
            values.put(COL_TITRE, livre.getTitre());
            return bdd.update(TABLE_LIVRES, values, COL_ID + " = " +id, null);
        }
    
        public int removeLivreWithID(int id){
                 return bdd.delete(TABLE_LIVRES, COL_ID + " = " +id, null);
        }
    
        public Livre getLivreWithTitre(String titre){
              Cursor c = bdd.query(TABLE_LIVRES, new String[] {COL_ID, COL_ISBN, COL_TITRE, COL_DATE}, COL_TITRE + " LIKE \"" + titre +"\"", null, null, null, null, null);
                return cursorToLivre(c);
        }
    
           private Livre cursorToLivre(Cursor c){
               if (c.getCount() == 0)
                return null;
    
            c.moveToFirst();
            Livre livre = new Livre();
            livre.setId(c.getInt(NUM_COL_ID));
            livre.setIsbn(c.getString(NUM_COL_ISBN));
            livre.setDate(c.getLong(NUM_COL_DATE));
            livre.setTitre(c.getString(NUM_COL_TITRE));
            c.close();
    
            return livre;
        }
    Classe MaBaseSQLite

    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
    public class MaBaseSQLite extends SQLiteOpenHelper {
    
        private static final String TABLE_LIVRES = "table_livres";
        private static final String COL_ID = "ID";
        private static final String COL_ISBN = "ISBN";
        private static final String COL_TITRE = "Titre";
        private static final String COL_DATE = "Date";
    
        private static final String CREATE_BDD = "CREATE TABLE " + TABLE_LIVRES + " ("
                + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_ISBN + " TEXT NOT NULL, "
                + COL_TITRE + " TEXT NOT NUL," + COL_DATE + " LONG);";
    
        public MaBaseSQLite(Context context, String name, CursorFactory factory, int version) {
            super(context, name, factory, version);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
                   db.execSQL(CREATE_BDD);
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE " + TABLE_LIVRES + ";");
            onCreate(db);
        }
    merci d'avance

  2. #2
    Membre Expert Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Par défaut
    salut,
    essaies de definir tes variables dans la classe LivresBDD comme ceci:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    private static final String COL_DATE = "COL_DATE";
     private static final String COL_ISBN = "COL_ISBN";
        private static final String COL_TITRE = "COL_TITRE";
    Eric

  3. #3
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2015
    Messages
    39
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2015
    Messages : 39
    Par défaut
    Cette fois, l'erreur est
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     ... table table_livres has no column named COL_DATE (code 1): , while compiling: INSERT INTO table_livres(COL_DATE,COL_TITRE,COL_ISBN) VALUES (?,?,?)
    je ne comprends pas pourquoi l'ordre des variables à insérer est autant aléatoire, pourtant la requete est bien

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    values.put(COL_ISBN, livre.getIsbn());
            values.put(COL_TITRE, livre.getTitre());
            values.put(COL_DATE, livre.getDate());

  4. #4
    Membre Expert Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Par défaut
    salut, mon erreur,
    essaies de changer la colonne Date par date1 et voir ce que ca donne.
    Eric

  5. #5
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2015
    Messages
    39
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2015
    Messages : 39
    Par défaut
    toujours le même genre d'erreur

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    E/SQLiteLog: (1) table table_livres has no column named date1
    E/SQLiteDatabase: Error inserting Titre=oui ISBN=bonjour date1=267278
    android.database.sqlite.SQLiteException: table table_livres has no column named date1 (code 1): , while compiling: INSERT INTO table_livres(Titre,ISBN,date1) VALUES (?,?,?)

  6. #6
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2015
    Messages
    39
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2015
    Messages : 39
    Par défaut
    J'ai copié tout ce projet (Livre) dans un autre projet identique (Livre2)
    j'ai corrigé NUL en NULL et changé LONG en INTEGER
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    private static final String CREATE_BDD = "CREATE TABLE " + TABLE_LIVRES + " ("
                + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_ISBN + " TEXT NOT NULL, "
                + COL_TITRE + " TEXT NOT NULL, " + COL_DATE + " INTEGER);";
    et ca marche, mais ca ne fait pas tout à fait mes affaires: si je dois chaque fois copier-coller mon code dans un autre projet j'ai pas fini..

Discussions similaires

  1. Erreur insert asp
    Par Alex35 dans le forum ASP
    Réponses: 4
    Dernier message: 03/01/2007, 14h24
  2. Ligne de commande : erreur INSERT
    Par webrider dans le forum Requêtes
    Réponses: 7
    Dernier message: 30/08/2006, 14h13
  3. [MySQL] erreur insertion quand je saisi des '
    Par snakejl dans le forum PHP & Base de données
    Réponses: 9
    Dernier message: 28/06/2006, 12h59
  4. Erreur INSERT id vide
    Par concombre dans le forum SQL Procédural
    Réponses: 2
    Dernier message: 26/05/2006, 17h23
  5. DBMemo sur champ Text erreur insert
    Par htristra dans le forum C++Builder
    Réponses: 1
    Dernier message: 02/11/2005, 15h17

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo