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