Run sql file with SQLiteDatabase Android

0

I am implementing in an Activiy the download of an sql file from a web server. The Download I already managed to do and it is working without problems. However, catching this downloaded file and having it executed with the execSQL () command is killing the application. Here is the code I'm using:

private static final String FOLDER ="filedownload";
private static final String FILE_SQL = "file.sql";
private SQLiteDatabase db;

Button to call class:

btnExecutar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                int insertCount = insertFromFile();
                Toast.makeText(SecondActivity.this, "Total de artigos inseridos: " + String.valueOf(insertCount), Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                Toast.makeText(SecondActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        }
    });

Class:

public int insertFromFile() throws IOException {
    int result = 0;

    File file = SecondActivity.this.getFileStreamPath(FOLDER+File.pathSeparator+FILE_SQL);
    FileInputStream insertsStream = new FileInputStream(file);
    BufferedReader insertReader = new BufferedReader(new InputStreamReader(insertsStream));

    while (insertReader.ready()) {
        String insertStmt = insertReader.readLine();
        this.db.execSQL(insertStmt);
        result++;
    }
    insertReader.close();

    return result;
}

It is killing the application, however the records are being inserted into the database. What could be wrong or how to do this procedure correctly?

Error Log

 --------- beginning of crash
04-25 21:55:18.274 1430-1430/myaplication.transition E/AndroidRuntime: FATAL 
EXCEPTION: main
Process: myaplication.transition, PID: 1430
android.database.sqlite.SQLiteException: not an error (code 0)
    at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
    at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:734)
    at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
    at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1679)
    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
    at myaplication.transition.SecondActivity.insertFromFile(SecondActivity.java:178)
    at myaplication.transition.SecondActivity$2.onClick(SecondActivity.java:87)
    at android.view.View.performClick(View.java:5640)
    at android.view.View$PerformClick.run(View.java:22455)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6165)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
    
asked by anonymous 26.04.2018 / 02:14

1 answer

1

To solve the problem I checked in the sql file that there were no empty lines, however there were some lines that were "broken" in two, as exemplified below:

INSERT INTO 'tab_status' ('id', 'status', 'descricao') VALUES (1,'Despachado', 
'Arquivo despachado');

The solution was to remove this "break" to leave a single line:

INSERT INTO 'tab_status' ('id', 'status', 'descricao') VALUES (1,'Despachado', 'Arquivo despachado');

In this way, while using the while to traverse the lines of the sql file, the system reads the entire line, line by line, and correctly inserts the data into the database.

    
26.04.2018 / 13:44