Run sql file with Sqlitedatabase Android

Asked

Viewed 335 times

0

I am implementing in an Activiy the download of an sql file from a server on the web. The Download I’ve managed to do and is working smoothly. However, taking this downloaded file and having it run with the execSQL() command is killing the application. Below follows the code I am using:

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

Button to call the 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’s killing the app, yet the records are being entered into the bank. What may 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)
  • What error is being returned to you in the Log lines that are displayed in Android Studio? His description makes it quicker to identify where the problem is.

  • @Leonardopaim edited the question and added the error log.

  • There is a face that is giving error when trying to run an empty line. It may be the last line of the file (before the EOF) or an intermediate line. Include a if checking that it is not empty before executing the command.

  • @Piovezan did not resolve with if. I checked the file also and there are no empty lines.

  • 2

    I suggest to log in or give println on each line before having run, any of them are causing the error. String.trim() can help at some point. If do not resolve post the file for the staff try to play.

  • @Piovezan, you were right! The problem was in the same file. There were no empty lines, however some lines were "broken" in two, which ended up generating reading error. By correcting this solved the problem. I thank you all!

  • Cool that worked correctly. I suggest now post in response form the solution you found. This makes it easier for the community to seek solutions.

Show 2 more comments

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');

This way when using while to traverse the rows of the sql file, the system reads the whole line, line by line, and correctly inserts the data into the database.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.