0

I am inserting some data in my SQLite database android. I created an ArrayList of Content values. Below is my code.

try {
                SQLiteDatabase db = this.getWritableDatabase();
                db.beginTransaction();
                for (int i = 0; i < arrayListAudio.size(); i++) {
                    db.insert(TABLE_AUDIO_FILES, null, arrayListAudio.get(i));
                }
                for (int i = 0; i < arrayListText.size(); i++) {
                    db.insert(TABLE_TEXT_FILES, null, arrayListText.get(i));
                }
                db.setTransactionSuccessful();
                db.endTransaction();
                db.close();
                //arrayListText.clear();
                //arrayListAudio.clear();
} catch (SQLException e) {
       Log.d("vijaysee", e.getMessage());
}

It works but sometimes it does not work and crashes the application

My problem is that it crashes the application directly because try/catch not working. If there is any problem in the code then it should call catch but instead, it crashes the application. I think that there should be some change in catch brackets like catch(// some change){} . Please help me with this.

2 Answers 2

1

You are catching a Java SQLException instead of an Android SQLiteException. And if you don't know what to catch, always try to catch an Exception. I'd rather worry about the SQL statement: INSERT INTO textFiles(null) VALUES (NULL) (which neither features columns nor values).

Sign up to request clarification or add additional context in comments.

4 Comments

does that mean, there are no columns in the table. Because I am deleting the tables and creating new each I open the activity.
It means this is not valid INSERT statement. You'd need to insert at least 1 column and 1 value.
ok it means i am inserting null values, so the problem is in the arrayList?
Likely it is, but you're also explicitly passing null into the method. There's also Room, but for learning it, it is better when you'd first learn SQLite and then Room (it's an abstraction layer with object relational mapping, which completely abstracts that all away, but under the hood nevertheless uses SQLite).
0

The reason why the app was crashing,

  1. I was not using synchronisation (in threads)
  2. I should catch using Exception class instead of specifying the exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.