Bonjour,
Dans une application que je suis en train de créer, j'ai ajouté des fonctionnalités pour :
- Effacer la base de données
- Réinitialiser la base de données
Suite à un test, je me retrouve devant un plantage.
Le logcat me dit :
Pour lancer la création de la base, j'ai :11-17 14:16:04.078: D/db(25903): myDbHelper.checkDatabase -> true
11-17 14:16:04.078: D/initializeDataBase : createDatabase(25903): false
11-17 14:16:04.088: E/SQLiteLog(25903): (11) database corruption at line 54216 of [00bb9c9ce4]
11-17 14:16:04.088: E/SQLiteLog(25903): (11) database disk image is malformed
11-17 14:16:04.088: E/DefaultDatabaseErrorHandler(25903): Corruption reported by sqlite on database: /data/data/fr.alphadroid.coursier/databases/Coursier
11-17 14:16:04.088: E/DefaultDatabaseErrorHandler(25903): !@ DB Corruption has happened before this
11-17 14:16:04.138: D/DbHelper(25903): onCreate -> createDatabase
11-17 14:16:04.138: D/DbHelper(25903): createDatabase -> copyDataBase
11-17 14:16:04.138: D/DbHelper(25903): copyDataBase -> myInput
11-17 14:16:04.138: D/DbHelper(25903): copyDataBase -> myOutput
11-17 14:16:04.138: D/DbHelper(25903): copyDataBase -> copyFile
11-17 14:16:04.138: D/FileHelper(25903): copyFile -> fromFile android.content.res.AssetManager$AssetInputStream@42a32de8 toFile java.io.FileOutputStream@42a32f08
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.148: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> write buffer 1024
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> toFile.flush
11-17 14:16:04.158: D/FileHelper(25903): copyFile -> toFile.close
11-17 14:16:04.168: D/FileHelper(25903): copyFile -> fromFile.close
11-17 14:16:04.168: D/DbHelper(25903): copyDataBase -> getWritableDatabase : close
11-17 14:16:04.228: I/Adreno-EGL(25903): <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: (CL3869936)
Voici la classe DbHelper.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 myDbHelper = new DbHelper(this); myDbHelper.createDatabase = true; myDbHelper.initializeDataBase(); myDbHelper.close();
La classe FileHelper.java
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 package fr.alphadroid.coursier; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; /** @author Danny Remington - MacroSolve * Helper class for sqlite database. **/ public class DbHelper extends SQLiteOpenHelper { /* The Android's default system path of the application * database in internal * storage. The package of the * application is part of the path of the * directory. */ private static String DB_DIR = "/data/data/fr.alphadroid.coursier/databases/"; private static String DB_NAME = "Coursier"; private static String DB_PATH = DB_DIR + DB_NAME; private static String OLD_DB_PATH = DB_DIR + "old_" + DB_NAME; private final Context myContext; public boolean createDatabase = false; private boolean upgradeDatabase = false; /** Constructor Takes and keeps a reference of the * passed context in order to * access to the * application assets and resources. * @param context **/ public DbHelper(Context context) { super(context, DB_NAME, null, context.getResources().getInteger(R.integer.databaseVersion)); // TODO Auto-generated constructor stub myContext = context; // Get the path of the database that is based on the context. DB_PATH = myContext.getDatabasePath(DB_NAME).getAbsolutePath(); } /** Upgrade the database in internal storage if it exists * but is not current. * Create a new empty database in * internal storage if it does not exist. **/ public boolean checkDatabase(){ SQLiteDatabase checkDB = null; try{ String myPath = DB_PATH; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); }catch(SQLiteException e){ //database does't exist yet. } if(checkDB != null){ checkDB.close(); } return checkDB != null ? true : false; } public void initializeDataBase() { Log.d("initializeDataBase : createDatabase",String.valueOf(createDatabase)); /* Creates or updates the database * in internal storage if it is needed * before opening the database. In all cases * opening the database copies the database in * internal storage to the cache. */ getWritableDatabase(); if (createDatabase) { /* * If the database is created by the copy method, * then the creation * code needs to go here. This method * consists of copying the new * database from assets into * internal storage and then caching it. */ try { /* * Write over the empty data that was created in internal * * storage with the one in assets and then cache it. * */ Log.d("DbHelper","createDatabase -> copyDataBase"); copyDataBase(); } catch (IOException e) { throw new Error("Error copying database"); } } else if (upgradeDatabase) { /* * * If the database is upgraded by the copy and reload method, then * * the upgrade code needs to go here. This method consists of * * renaming the old database in internal storage, create an empty * * new database in internal storage, copying the database from * * assets to the new database in internal storage, caching the new * * database from internal storage, loading the data from the old * * database into the new database in the cache and then deleting the * * old database from internal storage. */ try { FileHelper.copyFile(DB_PATH, OLD_DB_PATH); copyDataBase(); SQLiteDatabase old_db = SQLiteDatabase.openDatabase(OLD_DB_PATH, null, SQLiteDatabase.OPEN_READWRITE); SQLiteDatabase new_db = SQLiteDatabase.openDatabase(DB_PATH,null, SQLiteDatabase.OPEN_READWRITE); /* * Add code to load data into the new database from the old * * database and then delete the old database from internal * * storage after all data has been transferred. */ } catch (IOException e) { throw new Error("Error copying database"); } } } /** * * Copies your database from your local assets-folder to the just created * * empty database in the system folder, from where it can be accessed and * * handled. This is done by transfering bytestream. * */ private void copyDataBase() throws IOException { /* * * Close SQLiteOpenHelper so it will commit the created empty database * * to internal storage. */ /* * * Open the database in the assets folder as the input stream. * */ Log.d("DbHelper","copyDataBase -> myInput"); InputStream myInput = myContext.getAssets().open(DB_NAME); /* * Open the empty db in interal storage as the output stream. */ Log.d("DbHelper","copyDataBase -> myOutput"); OutputStream myOutput = new FileOutputStream(DB_PATH); /* * Copy over the empty db in internal storage with the database in the * * assets folder. */ Log.d("DbHelper","copyDataBase -> copyFile"); FileHelper.copyFile(myInput, myOutput); /* * Access the copied database so SQLiteHelper will cache it and mark it * * as created. */ Log.d("DbHelper","copyDataBase -> getWritableDatabase : close"); getWritableDatabase().close(); } /* * This is where the creation of tables and the initial population of the * * tables should happen, if a database is being created from scratch instead * * of being copied from the application package assets. Copying a database * * from the application package assets to internal storage inside this * * method will result in a corrupted database. * <P> * * NOTE: This method is normally only called when a database has not already * * been created. When the database has been copied, then this method is * * called the first time a reference to the database is retrieved after the * * database is copied since the database last cached by SQLiteOpenHelper is * * different than the database in internal storage. */ @Override public void onCreate(SQLiteDatabase db) { /* * Signal that a new database needs to be copied. The copy process must * * be performed after the database in the cache has been closed causing * * it to be committed to internal storage. Otherwise the database in * * internal storage will not have the same creation timestamp as the one * * in the cache causing the database in internal storage to be marked as * * corrupted. */ Log.d("DbHelper","onCreate -> createDatabase"); createDatabase = true; /* * This will create by reading a sql file and executing the commands in * * it. */ // try { // InputStream is = myContext.getResources().getAssets().open( // "create_database.sql"); // // String[] statements = FileHelper.parseSqlFile(is); // // for (String statement : statements) { // db.execSQL(statement); // } // } catch (Exception ex) { // ex.printStackTrace(); // } } /** * * Called only if version number was changed and the database has already * * been created. Copying a database from the application package assets to * * the internal data system inside this method will result in a corrupted * * database in the internal data system. */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { /* * Signal that the database needs to be upgraded for the copy method of * * creation. The copy process must be performed after the database has * * been opened or the database will be corrupted. */ upgradeDatabase = true; /* * Code to update the database via execution of sql statements goes * * here. */ /* * This will upgrade by reading a sql file and executing the commands in * it. */ // try { // InputStream is = myContext.getResources().getAssets().open( // "upgrade_database.sql"); // // String[] statements = FileHelper.parseSqlFile(is); // // for (String statement : statements) { // db.execSQL(statement); // } // } catch (Exception ex) { // ex.printStackTrace(); // } } /** * Called everytime the database is opened by getReadableDatabase or * * getWritableDatabase. This is called after onCreate or onUpgrade is * called. */ @Override public void onOpen(SQLiteDatabase db) { super.onOpen(db); //} /* * Add your public helper methods to access and get content from the * * database. You could return cursors by doing * * "return myDataBase.query(....)" so it'd be easy to you to create adapters * for your views. */ } }
Quelqu'un saurait-il m'indiquer comment corriger ce problème ?
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 package fr.alphadroid.coursier; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; import java.nio.channels.FileChannel; import android.util.Log; /** * @author Danny Remington - MacroSolve * Helper class for common tasks using files. * */ public class FileHelper { /** * Creates the specified <i><b>toFile</b></i> * that is a byte for byte a copy * of <i><b>fromFile</b></i>. * If <i><b>toFile</b></i> already existed, then * it will be * replaced with a copy of <i><b>fromFile</b></i>. The name and * path of <i><b>toFile</b></i> will be that of <i><b>toFile</b></i>. * Both * <i><b>fromFile</b></i> and <i><b>toFile</b></i> will be * closed by this operation. * * @param fromFile * - InputStream for the file to copy from. * * @param toFile * - InputStream for the file to copy to. * */ public static void copyFile(InputStream fromFile, OutputStream toFile) throws IOException { Log.d("FileHelper","copyFile -> fromFile "+fromFile+" toFile "+toFile); // transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; try { while ((length = fromFile.read(buffer)) > 0) { Log.d("FileHelper","copyFile -> write buffer "+length); toFile.write(buffer, 0, length); } } // Close the streams finally { try { if (toFile != null) { try { Log.d("FileHelper","copyFile -> toFile.flush"); toFile.flush(); } finally { Log.d("FileHelper","copyFile -> toFile.close"); toFile.close(); } } } finally { if (fromFile != null) { Log.d("FileHelper","copyFile -> fromFile.close"); fromFile.close(); } } } } /** * Creates the specified <i><b>toFile</b></i> * that is a byte for byte a copy * of <i><b>fromFile</b></i>. If <i><b>toFile</b></i> * already existed, then * it will be replaced * with a copy of <i><b>fromFile</b></i>. * The name and * path of <i><b>toFile</b></i> * will be that of <i><b>toFile</b></i>. * Both * <i><b>fromFile</b></i> and <i><b>toFile</b></i> * will be closed by this * operation. * * * @param fromFile * - String specifying the path of the file to copy from. * * @param toFile * - String specifying the path of the file to copy to. */ public static void copyFile(String fromFile, String toFile) throws IOException { copyFile(new FileInputStream(fromFile), new FileOutputStream(toFile)); } /** * Creates the specified <i><b>toFile</b></i> * that is a byte for byte a copy * * of <i><b>fromFile</b></i>. * If <i><b>toFile</b></i> already existed, then * it will be replaced with a copy of <i><b>fromFile</b></i>. * The name and * path of <i><b>toFile</b></i> will be * that of <i><b>toFile</b></i>. Both * <i><b>fromFile</b></i> * and <i><b>toFile</b></i> will be closed by this * operation. * * * @param fromFile * - File for the file to copy from. * * @param toFile * - File for the file to copy to. */ public static void copyFile(File fromFile, File toFile) throws IOException { copyFile(new FileInputStream(fromFile), new FileOutputStream(toFile)); } /** * Creates the specified <i><b>toFile</b></i> that is a byte for byte * a copy * of <i><b>fromFile</b></i>. If <i><b>toFile</b></i> already * existed, then * it will be replaced with a copy of <i><b>fromFile</b></i>. * The name and * path of <i><b>toFile</b></i> will be that of <i><b>toFile</b></i>. * Both * <i><b>fromFile</b></i> and <i><b>toFile</b></i> will be closed by this * operation. * * @param fromFile * - FileInputStream for the file to copy from. * * @param toFile * - FileInputStream for the file to copy to. */ public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException { FileChannel fromChannel = fromFile.getChannel(); FileChannel toChannel = toFile.getChannel(); try { fromChannel.transferTo(0, fromChannel.size(), toChannel); } finally { try { if (fromChannel != null) { fromChannel.close(); } } finally { if (toChannel != null) { toChannel.close(); } } } } /** * Parses a file containing sql statements into a String array that contains * only the sql statements. Comments and white spaces in the file are not * parsed into the String array. Note the file must not contained malformed * comments and all sql statements must end with a semi-colon ";" in order * for the file to be parsed correctly. The sql statements in the String * array will not end with a semi-colon ";". * * * @param sqlFile * - String containing the path for the file that contains sql * statements. * * * @return String array containing the sql statements. */ public static String[] parseSqlFile(String sqlFile) throws IOException { return parseSqlFile(new BufferedReader(new FileReader(sqlFile))); } /** * Parses a file containing sql statements into a String array that contains * only the sql statements. Comments and white spaces in the file are not * parsed into the String array. Note the file must not contained malformed * comments and all sql statements must end with a semi-colon ";" in order * for the file to be parsed correctly. The sql statements in the String * array will not end with a semi-colon ";". * * * @param sqlFile * - InputStream for the file that contains sql statements. * * * @return String array containing the sql statements. */ public static String[] parseSqlFile(InputStream sqlFile) throws IOException { return parseSqlFile(new BufferedReader(new InputStreamReader(sqlFile))); } /** * Parses a file containing sql statements into a String array that contains * only the sql statements. Comments and white spaces in the file are not * parsed into the String array. Note the file must not contained malformed * comments and all sql statements must end with a semi-colon ";" in order * for the file to be parsed correctly. The sql statements in the String * array will not end with a semi-colon ";". * * * @param sqlFile * - Reader for the file that contains sql statements. * * * @return String array containing the sql statements. */ public static String[] parseSqlFile(Reader sqlFile) throws IOException { return parseSqlFile(new BufferedReader(sqlFile)); } /** * Parses a file containing sql statements into a String array that contains * only the sql statements. Comments and white spaces in the file are not * parsed into the String array. Note the file must not contained malformed * comments and all sql statements must end with a semi-colon ";" in order * for the file to be parsed correctly. The sql statements in the String * array will not end with a semi-colon ";". * * * @param sqlFile * - BufferedReader for the file that contains sql statements. * * * @return String array containing the sql statements. */ public static String[] parseSqlFile(BufferedReader sqlFile) throws IOException { String line; StringBuilder sql = new StringBuilder(); String multiLineComment = null; while ((line = sqlFile.readLine()) != null) { line = line.trim(); // Check for start of multi-line comment if (multiLineComment == null) { // Check for first multi-line comment type if (line.startsWith("/*")) { if (!line.endsWith("}")) { multiLineComment = "/*"; } // Check for second multi-line comment type } else if (line.startsWith("{")) { if (!line.endsWith("}")) { multiLineComment = "{"; } // Append line if line is not empty or a single line comment } else if (!line.startsWith("--") && !line.equals("")) { sql.append(line); } // Check for matching end comment } else if (multiLineComment.equals("/*")) { if (line.endsWith("*/")) { multiLineComment = null; } // Check for matching end comment } else if (multiLineComment.equals("{")) { if (line.endsWith("}")) { multiLineComment = null; } } } sqlFile.close(); return sql.toString().split(";"); } }
Merci d'avance pour votre aide.
Partager