Android Question Simple 2 SQlite example (by Klaus)

Beja

Expert
Licensed User
Longtime User
This is about the second example with more functions.

1- First run just after installation is ok
2- next run (run 2) brings exception
LogCat connected to: emulator-5554
** Activity (main) Create, isFirst = true **


** Activity (main) Resume **
ExecuteHtml: SELECT ID, FirstName As [First name], LastName As [Last name], City FROM persons
** Activity (main) Pause, UserClosed = false **
** Activity (edit) Create, isFirst = true **
** Activity (edit) Resume **
** Activity (edit) Pause, UserClosed = true **
** Activity (main) Resume **
ExecuteHtml: SELECT ID, FirstName As [First name], LastName As [Last name], City FROM persons
** Activity (main) Pause, UserClosed = false **
** Activity (edit) Create, isFirst = false **
** Activity (edit) Resume **
** Activity (edit) Pause, UserClosed = true **
** Activity (main) Resume **
ExecuteHtml: SELECT ID, FirstName As [First name], LastName As [Last name], City FROM persons
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
java.lang.IllegalStateException: database not open
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1333)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
at anywheresoftware.b4a.sql.SQL.ExecQuery2(SQL.java:167)
at anywheresoftware.b4a.sql.SQL.ExecQuery(SQL.java:155)
at b4a.sqlitelight2.main._executehtml(main.java:384)
at b4a.sqlitelight2.main._showtable(main.java:573)
at b4a.sqlitelight2.main._activity_resume(main.java:327)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:158)
at b4a.sqlitelight2.main.afterFirstLayout(main.java:104)
at b4a.sqlitelight2.main.access$100(main.java:16)
at b4a.sqlitelight2.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
java.lang.IllegalStateException: database not open

Note:
after the exception I have two options: Continue, yes or no
If I chose yes, database is blank, and next run will be the same error
If I chose No, it will exit the App, but next run the data is there but any editing or added records are lost


Note: no modification.
 
Last edited:

Beja

Expert
Licensed User
Longtime User
Sorry, Beja, what means "with more functions"?

Thanks LukaMs, this is how Klaus described it "with more functions" (check Klaus signature and follow the link)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Maybe I was right about a question that I made with regard to the "exits" from the activities (without use Activity.Finish); I think is possible that a dirty queue remains open.

I think so, because, adding:
ExitApplication

in the Activity_Pause, the app has no problems.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
In fact I didn't touch the code, that was a quick run to see.. but I think one of the activities (that you visit, like Add) is not closing. Will also check.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
No, If I only add Activity.Finish in ever Activity but don't add ExitApplication, I get the error.

I confirm that adding ExitApplication, the app works well.

For some reason, isFirst is False; this means that the app is running, whithout use ExitApplication
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Yeah,
Now working fine.. but edit and add are not saving.. when you close and open again, the original db comes up.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Yes, but that is because of design choice, because it is an example.
You should delete:
B4X:
        File.Delete(File.DirInternal, "persons.db") ' only for testing, removes the database

I found that the app is still running (isFirst remains False) because of the WebView.

I have tested it in an empty project and this error remains.

[EDIT] It is normal behavior when you close the Main via the hardware Back button, so it is not inherent to the WebView.
 
Last edited:
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks LucaMS, but I don't know how to save and update the table..
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
B4X:
    If FirstTime Then
'        File.Delete(File.DirInternal, "persons.db") ' only for testing, removes the database

B4X:
Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        ExitApplication
    End If
End Sub

These two changes are sufficient.

Obviously, you should start the Activity for the editing pressing [Edit] and save pressing [Update]
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry for answering somewhat late, but I had no internet connection for two days.

Add the three lines below at the beginning of Activity_Resume.
B4X:
Sub Activity_Resume
    If SQL1.IsInitialized = False Then
        SQL1.Initialize(File.DirInternal, "persons.db", True)
    End If
The first post in SQLiteLight has been updated.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks Klaus,
I downloaded the updated version..
I want to edit the table to add 3 more fields (state, zip and phone) but didn't know how to do that.
Any hint appreciated.
 
Upvote 0
Top