Android Question Problem making a SQLite query

davidvidasoft

Member
Licensed User
I'm testing how a SQLite query works, here is a test code I was trying:

B4X:
Sub btnTest_Click
    Dim dir As String = DBUtils.CopyDBFromAssets("test.db")
    SQL.Initialize(dir, "test.db", True)

    Dim SQLQuery As Map


    Dim string2(0) As List
    Dim list3 As List
    string2 = DBUtils.ExecuteMemoryTable(SQL, "SELECT * FROM PERSONA", Null, 0).Get(0)
    list3.Initialize2(string2)
    Msgbox(list3, "Primer resultset")
    '' INPUT?
    InputList(string2, "title", -1)
    'Return

    .
    .
    .
End Sub

And it crashes somehow before showing anything,

B4X:
** Activity (login) Pause, UserClosed = false **
** Activity (storev2) Create, isFirst = true **
** Activity (storev2) Resume **
ExecuteMemoryTable: SELECT * FROM PERSONA
ExecuteMemoryTable: SELECT * FROM PERSONA
ExecuteJSON: select * from Persona
ExecuteMemoryTable: SELECT * FROM PERSONA
Error occurred on line: 191 (DBUtils)
java.lang.ClassCastException: java.lang.String[] cannot be cast to anywheresoftware.b4a.objects.collections.List[]
    at b4a.mayorio.tienda.storev2._btntest_click(storev2.java:690)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:5647)
    at android.view.View$PerformClick.run(View.java:22465)
    at android.os.Handler.handleCallback(Handler.java:761)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:156)
    at android.app.ActivityThread.main(ActivityThread.java:6577)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)

If I put a new line before the InputList(....) line of code the whole code works properly.

I am obviouly doing something wrong here but don't know what. Also, I know that two first lines should have gone in the Activity_Create() sub but didn't want to mess up the project and it had been working all good like this until I added that InputList() line.
 
Last edited:

Computersmith64

Well-Known Member
Licensed User
Longtime User
If a .Get is even legal on a memory table (I can't check it because I'm in the process of rebuilding my development machine), your receiver (string2) would have to be declared as a String (not a List). Also string2(0) is declaring a zero length array (ie: there are no elements) - so it won't work anyway.

This is how I would write it:
B4X:
Sub btnTest_Click
    Dim dir As String = DBUtils.CopyDBFromAssets("test.db")SQL.Initialize(dir, "test.db", True)
    Dim SQLQuery AsMap

    Private string2 as List = DBUtils.ExecuteMemoryTable(SQL, "SELECT * FROM PERSONA", Null, 0)
    Msgbox(string2.Get(0), "Primer resultset")
    
'I'm not sure what you're trying to do here, so I won't change it.
'' INPUT?
    InputList(string2, "title", -1)'Return
 .
 .
 .End Sub

Like I said, I can't test anything at the moment so I can't say for sure whether a .Get on a memory table is legal (but I suspect it's not)...

- Colin.
 
Upvote 0

davidvidasoft

Member
Licensed User
Yeah, the InputList was part of a dumbtesting as I was experimenting with those objects to see what I got from them.

yK162R9.jpg


The thing is that ExecuteMemoryTable returns a List filled with array of strings so I first used a normal List but it gave me problems
QktRKgM.jpg

I declared it as Dim string(0) As List to stop the problem, it worked. And I was getting correct values from my query until I deleted a new line I had put above the InputList(). I thought it was nasty getting an error from deleting a new line so I made this post.

Thanks for your time. I'll just avoid writing the initialization inside button events since it is a bad practice anyway :D and stops the app from crashing
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Please note (you can see it in the screenshot of the documentation for ExecuteMemoryTable) that ExecuteMemoryTable returns a list of arrays. The individual array can then be extracted with the Get method. Please note that the returned array is an array of strings (as you noted yourself). So the correct declaration for string 2 would be

B4X:
Dim string2() As String ' Array of strings received from the Get method

P.S.: Colin's (aka @Computersmith64) code is good too, he just was not able to test if you could execute Get directly from ExecuteMemoryTable, else he would have come to the same conclusion.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Please note (you can see it in the screenshot of the documentation for ExecuteMemoryTable) that ExecuteMemoryTable returns a list of arrays. The individual array can then be extracted with the Get method. Please note that the returned array is an array of strings (as you noted yourself). So the correct declaration for string 2 would be

B4X:
Dim string2() As String ' Array of strings received from the Get method

P.S.: Colin's (aka @Computersmith64) code is good too, he just was not able to test if you could execute Get directly from ExecuteMemoryTable, else he would have come to the same conclusion.

Yes (thanks OliverA) - although I did point out that Dim string2(0) would create a zero element array, I should probably have specified the correct way to declare an array... :)

- Colin.
 
Upvote 0
Top