Bonjour,
Je dois réaliser une application permettant d'enregistrer des notes en base de données. A l'insertion dans une table, je dois enregistrer uniquement le titre. Le contenu doit être inséré par un update.
J'ai déjà pas mal avancé, mais après plusieurs recherche, je n'arrive pas à enregistrer en base de données, le code bloque à la création de la table. Pourtant, il me semble que la syntaxe est correcte.
Test de l'application :
Voici l'Activity principale, section de création de la boite de dialogue personnalisée avec bouton de validation
DataSource de la base de données :
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 ... builder/*.setMessage(R.string.dialog_message) */.setTitle(R.string.dialog_title) .setView(inflater.inflate(R.layout.layout_dialog_add, null)); final EditText titreEditable = (EditText)DialogView.findViewById(R.id.titleNote); // Add the buttons builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User clicked OK button String titre; titre = titreEditable.getText().toString(); notes note = datasource.createNote(titre); adapter.add(note); adapter.notifyDataSetChanged(); dialog.dismiss(); Context context = getApplicationContext(); CharSequence text = "Note sauvegardée"; int duration = LENGTH_SHORT; Toast toast = makeText(context, text, duration); toast.show(); } }); ...
OpenHelper
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 package fr.iut_amiens.nicolas.td3; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * Created by Nicolas on 06/02/2015. */ public class notesDataSource { // Database fields private SQLiteDatabase database; private DatabaseOpenHelper dbHelper; private String[] allColumns = { DatabaseOpenHelper.COLUMN_ID, DatabaseOpenHelper.COLUMN_TITLE }; public notesDataSource(Context context) { dbHelper = new DatabaseOpenHelper(context); } public void open() throws SQLException { database = dbHelper.getWritableDatabase(); } public void read() throws SQLException { database = dbHelper.getReadableDatabase(); } public void close() { dbHelper.close(); } public void deleteTable() { dbHelper.deleteTable(database);} public notes createNote(String titre) { ContentValues values = new ContentValues(); notes note = new notes(titre); values.put(DatabaseOpenHelper.COLUMN_TITLE, note.getTitle()); values.put(DatabaseOpenHelper.COLUMN_CONTENT, note.getContent()); long insertId = database.insert(DatabaseOpenHelper.DATABASE_TABLE_NOTES, null, values); Cursor cursor = database.query(DatabaseOpenHelper.DATABASE_TABLE_NOTES, allColumns, DatabaseOpenHelper.COLUMN_ID + " = " + insertId, null, null, null, null); cursor.moveToFirst(); notes newNote = cursorToNote(cursor); cursor.close(); return newNote; } public void deleteNote(notes note) { long id = note.getId(); System.out.println("Note deleted with id: " + id); database.delete(DatabaseOpenHelper.DATABASE_TABLE_NOTES, DatabaseOpenHelper.COLUMN_ID + " = " + id, null); } public List<notes> getAllNotes() { List<notes> listNotes = new ArrayList<notes>(); Cursor cursor = database.query(DatabaseOpenHelper.DATABASE_TABLE_NOTES, new String[] {"_id", "title", "content"}, null, null, null, null, null); cursor.moveToFirst(); while (!cursor.isAfterLast()) { notes notes = cursorToNote(cursor); listNotes.add(notes); cursor.moveToNext(); } // make sure to close the cursor cursor.close(); return listNotes; } private notes cursorToNote(Cursor cursor) { notes notes = new notes(); notes.setId(cursor.getLong(0)); notes.setTitle(cursor.getString(1)); notes.setContent(cursor.getString(2)); return notes; } }
Logcat :
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 package fr.iut_amiens.nicolas.td3; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; /** * Created by Nicolas on 06/02/2015. */ public class DatabaseOpenHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 2; public static final String DATABASE_TABLE_NOTES= "Notes"; public static final String COLUMN_ID = "_id"; public static final String COLUMN_TITLE= "title"; public static final String COLUMN_CONTENT= "content"; public static final String DATABASE_NAME= "db.note"; private static final String DATABASE_TABLE_CREATE = "CREATE TABLE " + DATABASE_TABLE_NOTES + " (" + COLUMN_ID + " integer primary key autoincrement, " + COLUMN_TITLE + " TEXT NOT NULL, " + COLUMN_CONTENT+ " TEXT ) ; "; public DatabaseOpenHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_TABLE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(DatabaseOpenHelper.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_NOTES); onCreate(db); } public void deleteTable(SQLiteDatabase db){ db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_NOTES); } }
Quelqu'un saurait-il m'indiquer comment résoudre ce problème ?02-14 16:37:36.460 5177-5177/fr.iut_amiens.nicolas.td3 I/art﹕ Late-enabling -Xcheck:jni
02-14 16:37:37.254 5177-5218/fr.iut_amiens.nicolas.td3 D/OpenGLRenderer﹕ Render dirty regions requested: true
02-14 16:37:37.302 5177-5177/fr.iut_amiens.nicolas.td3 D/Atlas﹕ Validating map...
02-14 16:37:37.382 5177-5218/fr.iut_amiens.nicolas.td3 I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/28/14, c33033c, Ia6306ec328
02-14 16:37:37.385 5177-5218/fr.iut_amiens.nicolas.td3 I/OpenGLRenderer﹕ Initialized EGL, version 1.4
02-14 16:37:37.461 5177-5218/fr.iut_amiens.nicolas.td3 D/OpenGLRenderer﹕ Enabling debug mode 0
02-14 16:37:39.434 5177-5177/fr.iut_amiens.nicolas.td3 W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed.
02-14 16:37:39.436 5177-5177/fr.iut_amiens.nicolas.td3 W/ViewRootImpl﹕ Dropping event due to root view being removed: MotionEvent { action=ACTION_UP, id[0]=0, x[0]=988.03564, y[0]=-541.4286, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=192350469, downTime=192350425, deviceId=6, source=0x1002 }
02-14 16:37:39.436 5177-5177/fr.iut_amiens.nicolas.td3 W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed.
02-14 16:37:39.436 5177-5177/fr.iut_amiens.nicolas.td3 W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection
02-14 16:37:39.448 5177-5177/fr.iut_amiens.nicolas.td3 W/IInputConnectionWrapper﹕ requestCursorAnchorInfo on inactive InputConnection
02-14 16:37:39.452 5177-5177/fr.iut_amiens.nicolas.td3 W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection
02-14 16:37:39.619 5177-5177/fr.iut_amiens.nicolas.td3 W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection
02-14 16:37:43.203 5177-5177/fr.iut_amiens.nicolas.td3 E/SQLiteLog﹕ (1) no such table: Notes
02-14 16:37:43.227 5177-5177/fr.iut_amiens.nicolas.td3 E/SQLiteDatabase﹕ Error inserting title= content=null
android.database.sqlite.SQLiteException: no such table: Notes (code 1): , while compiling: INSERT INTO Notes(title,content) VALUES (?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
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.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at fr.iut_amiens.nicolas.td3.notesDataSource.createNote(notesDataSource.java:46)
at fr.iut_amiens.nicolas.td3.MainActivity$2.onClick(MainActivity.java:130)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:160)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-14 16:37:43.227 5177-5177/fr.iut_amiens.nicolas.td3 E/SQLiteLog﹕ (1) no such table: Notes
02-14 16:37:43.228 5177-5177/fr.iut_amiens.nicolas.td3 D/AndroidRuntime﹕ Shutting down VM
02-14 16:37:43.231 5177-5177/fr.iut_amiens.nicolas.td3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: fr.iut_amiens.nicolas.td3, PID: 5177
android.database.sqlite.SQLiteException: no such table: Notes (code 1): , while compiling: SELECT _id, title FROM Notes WHERE _id = -1
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
at fr.iut_amiens.nicolas.td3.notesDataSource.createNote(notesDataSource.java:48)
at fr.iut_amiens.nicolas.td3.MainActivity$2.onClick(MainActivity.java:130)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:160)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-14 16:37:55.197 5177-5177/fr.iut_amiens.nicolas.td3 I/Process﹕ Sending signal. PID: 5177 SIG: 9
Merci d'avance pour votre aide.
Partager