Reputation: 11878
Can anybody explain why:
db.execSQL("DROP TABLE IF EXISTS Definition;");
db.execSQL("CREATE TABLE Definition (txt TEXT);");
works well, but
db.execSQL("DROP TABLE IF EXISTS Definition; CREATE TABLE Definition (txt TEXT);");
does NOTHING (no table "Definition" exists after this exec).
(db is instance of SQLiteDatabase)
P.S. In iOS both variants do absolutely the same. As well as in SQLite manager (Firefox add-on).
Upvotes: 1
Views: 64
Reputation: 4214
SQLite doesn't want to execute several commands at a time. I write all the commands into a text file, read lines until ";" is met and execute one by one in a loop until readLine is null;
List<String> sqlExpressions = new ArrayList<String>();
try {
// read table creation sql script from the file
InputStream inStream = mContext.getResources().getAssets().open("createTables.sql");
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
String line = null;
while((line = reader.readLine()) != null) {
sb.append(line + "\n");
if(line.lastIndexOf(";") > 0) {
sqlExpressions.add(sb.toString());
sb = new StringBuilder();
}
}
reader.close();
} catch (Exception e) {
Log.d("DB_Creation", e.getMessage());
}
for (String sqlExpr : sqlExpressions) {
database.execSQL(sqlExpr);
}
Upvotes: 4