I'm developing an Android
app which in I have a SQLite
dump file in assets and I'm trying to create a database in user's phone using that dump.
The problem is it's a long dump file containing sql command including line breaks like this:
CREATE TABLE [tblType] (
[IdType] INTEGER NOT NULL ON CONFLICT ROLLBACK PRIMARY KEY ON CONFLICT ROLLBACK AUTOINCREMENT,
[Type] NVARCHAR);
So when I try to create the database using SQLiteOpenHelper
:
SQLiteOpenHelper openHelper0 = new DbHelper(this);
Cursor cu = null;
try {
cu = openHelper0.getReadableDatabase().rawQuery("select IdWord,Word from tblWord", null);
} catch (Exception e) {
e.printStackTrace();
}
I get syntax error on every line including a line break:
android.database.sqlite.SQLiteException: near "(": syntax error (code 1): , while compiling: CREATE TABLE [tblType](
And if I remove the first line break I get:
android.database.sqlite.SQLiteException: near ",": syntax error (code 1): , while compiling: CREATE TABLE [tblType]([IdType] INTEGER NOT NULL ON CONFLICT ROLLBACK PRIMARY KEY ON CONFLICT ROLLBACK AUTOINCREMENT,
As I said, it's a large dump and I can't manually remove all linebreaks; So the question is is there any way like configurations or extra code to make Android
ignores the line breaks?
This is the main code from DBHelper
:
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
try {
InputStream is = activity.getAssets().open("am.sql");
Scanner scanner = new Scanner(is).useDelimiter("\n");
while (scanner.hasNext()) {
sqLiteDatabase.execSQL(scanner.next());
}
is.close();
} catch (IOException ex){
}
}
You are reading the file line by line. This does not work for SQL statements having multiple lines.
You can use a semicolon at the end of a line to detect the actual end of statements, if you do not have any SQL statements that contain embedded semicolons (triggers, or text values with a semicolon before a newline).