Android Question "Order by" in Db does not work

Giusy

Active Member
Licensed User
Hi,
Mydb contains the table TABELLA_TIPI with two fields: TIPO and DESCRIZIONE.
This is the code:
B4X:
If File.Exists(File.DirRootExternal, "MYDB") = False Then
    File.Copy(File.DirAssets, "MYDB.DB", File.DirDefaultExternal, "MYDB.DB")
        SQL1.Initialize(File.DirDefaultExternal, "MYDB.DB", True)
        Dim SenderFilter As Object = SQL1.ExecQueryAsync("SQL", "SELECT * FROM TABELLA_TIPI", Null)
        Wait For (SenderFilter) SQL_QueryComplete (Success As Boolean, rs As ResultSet)
        If Success Then
        ...
If I change the query in
"SELECT * FROM TABELLA_TIPI ORDER BY TIPO"
or
"SELECT * FROM TABELLA_TIPI ORDER BY DESCRIZIONE"

It does not change anything.

Thank you
 

OliverA

Expert
Licensed User
Could it be that when you run it for the first time, it creates the db and runs the order by, but when you run it the second time, it does not copy the db and it does not run the query, therefore creating strange results (even if you change the query, if the db already exits, it is not run).
 

OliverA

Expert
Licensed User
hmm, the db is usually only copied once.
The only time the OP copies the db is if it is not found at File.DirRootExternal. After the first time, if the db is not removed, any second, third, fourth, etc running of the app will not copy the db. It will also not run the SELECT/ORDER BY query. I was just wondering if that was the issue: that the db has already been copied and therefore will never run the SELECT/ORDER BY query again unless there is no db found on File.DirRootExternal.

Clarification: I'm not suggesting the OP should copy the file again. I'm just pointing out that there may be logical fallacy when it comes to running the SELECT/ORDER BY query, since it will only run after the db has been copied. If a db is already in place, the SELECT/ORDER BY will not be run. This could lead to unexpected results.
 

Mahares

Well Known Member
Licensed User
In the first line you use: File.DirRootExternal and in the rest if the code you use: File.DirDefaultExternal. You have to be consistent.
If you are using targetSDK of 26 in the manifest, you need runtimepermissions library. Then you can use rp.GetSafeDirDefaultExternal("") instead of: File.DirDefaultExternal
 
Top