Android Tutorial DBUtils - Android databases are now simple!

Declan

Well-Known Member
Licensed User
Longtime User
I am using the DBUtils eample and editing it for use in my app.
I am attempting to change the id field in the students table to an alpha text string.
I have a EditText "txtChangeID" and a Button "btnChangeID"
My Code is:
B4X:
Sub btnChangeID_click
    Dim WhereFields As Map
    WhereFields.Initialize
    WhereFields.Put("id", spnrStudentId.SelectedItem)
    DBUtils.UpdateRecord(SQL, "students", "id", txtChangeID.Text, WhereFields)
   
    'Fill the students id spinner (aka combo box).
    DBUtils.ExecuteSpinner(SQL, "SELECT Id FROM Students", Null, 0, spnrStudentId)
    spnrStudentId_ItemClick(0, spnrStudentId.GetItem(0))
End Sub

The above works and the new id is written to the table correctly.
However, when I select the "new" text string id I get the following error:
An error has occurred in
sub:main_spnrstudentid_itemclick (java line:
485)
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
Continue?

The code in spnrStudentId_Itemclick is:
B4X:
Sub spnrStudentId_ItemClick (Position As Int, Value As Object)
    Dim m As Map
    m = DBUtils.ExecuteMap(SQL, "SELECT Id, [First Name], [Last Name], Birthday FROM students WHERE id = ?", _
        Array As String(Value))
    If m = Null Or m.IsInitialized = False Then 'Null will return if there is no match
        lblStudentName.Text = "N/A"
        lblBirthday.Text = ""
    Else
        lblStudentName.Text = m.Get("first name") & " " & m.Get("last name") 'keys are lower cased!
        lblBirthday.Text = DateTime.Date(m.Get("birthday"))
    End If
    'Get the tests for this specific student (currently it is all tests).
    DBUtils.ExecuteSpinner(SQL, "SELECT test FROM Grades WHERE id = ?", _
        Array As String(Value), 0, spnrTests)
    spnrTests.SelectedIndex = 0
    spnrTests_ItemClick(0, spnrTests.GetItem(0))
    'FindFailedTests(Value)
End Sub
 

Computersmith64

Well-Known Member
Licensed User
Longtime User

Where is the app crashing? It seems that you are assuming it's spnrStudentId that is causing it, but could it be the call you are making to spnrTests_ItemClick on the last line of your spnrStudentId_ItemClick sub? Are there records in the Grades table with a matching id?

Actually, looking at it again, if it's a problem with 0 records coming from your Grades table, the crash would be caused in your spnrTests.SelectedIndex = 0 line because the spinner will be empty.

- Colin.
 

Declan

Well-Known Member
Licensed User
Longtime User
Thanks Colin,
That was it.
I changed some code
B4X:
    Dim WhereFields As Map
    WhereFields.Initialize
    WhereFields.Put("id", spnrStudentId.SelectedItem)
    DBUtils.UpdateRecord(SQL, "students", "id", txtChangeID.Text, WhereFields)
    DBUtils.UpdateRecord(SQL, "Grades", "id", txtChangeID.Text, WhereFields)

This also writes the new ID to the Grades table.
 

wonder

Expert
Licensed User
Longtime User
Hello, hello!

Can anyone help me modify the ExecuteMemoryTable method into asynchronous mode?
Many thanks in advance!

B4X:
'Executes the query and returns the result as a list of arrays.
'Each item in the list is a strings array.
'StringArgs - Values to replace question marks in the query. Pass Null if not needed.
'Limit - Limits the results. Pass 0 for all results.
Sub ExecuteMemoryTable(SQL As SQL, Query As String, StringArgs() As String, Limit As Int) As List
    Dim cur As Cursor
    If StringArgs <> Null Then
        cur = SQL.ExecQuery2(Query, StringArgs)
    Else
        cur = SQL.ExecQuery(Query)
    End If
    Log("ExecuteMemoryTable: " & Query)
    Dim table As List
    table.Initialize
    If Limit > 0 Then Limit = Min(Limit, cur.RowCount) Else Limit = cur.RowCount
    For row = 0 To Limit - 1
        cur.Position = row
        Dim values(cur.ColumnCount) As String
        For col = 0 To cur.ColumnCount - 1
            values(col) = cur.GetString2(col)
        Next
        table.Add(values)
    Next
    cur.Close
    Return table
End Sub
 

konradwalsh

Active Member
Licensed User
Longtime User
I read this entire thread and checked the examples but cant see how I store a double or decimal..
Maybe thats what real is but not sure

Nevermind... i wasnt testing real correctly
 
Last edited:

MbedAndroid

Well-Known Member
Licensed User
Longtime User
dont use a real, but text instead. In that case you will get more precision. With a real the fraction is limited, see some topics about this
 

StefanoAccorsi

Member
Licensed User
Longtime User
Hi there. Strange issue with CopyDBFromAssets ...

I put my sqlite file in Files tab. Then run:

B4X:
If FirstTime Then
   targetDbDir = DBUtils.CopyDBFromAssets("xxx.sqlite")
   SQLHH.Initialize(targetDbDir, "xxx.sqlite", False)
End If

but I got an error inside CopyDBFromAssets:

B4X:
java.io.FileNotFoundException: /data/user/0/b4a.example/files/virtual_assets/xxx.sqlite: open failed: ENOENT (No such file or directory)

I modified the CopyDBFromAssets as follows:

B4X:
1. Public Sub CopyDBFromAssets (FileName As String) As String
2.     Dim TargetDir As String
3.     If File.ExternalWritable Then TargetDir = File.DirDefaultExternal Else TargetDir = File.DirInternal
4.     If File.Exists(TargetDir, FileName) = False Then
5.         If File.Exists(File.DirAssets, FileName) Then
6.             File.Copy(File.DirAssets, FileName, TargetDir, FileName)
7.         End If
8.     End If
9.     Return TargetDir
10.End Sub

just to find out that the line 5 returns True while the line 6 generates the error reported above.

How is it possible? Thank you.
 

StefanoAccorsi

Member
Licensed User
Longtime User
@Erel I modified the code in DBUtils only to check the strange behaviour I described. Anyway I totally changed the code, for now: for testing purposes I created and populated the DB programmarically. Anyway I created another project and as first thing I tried the code above: no errors this time.

Probably something went wrong with the other project: I tried what Erel suggested but the error was always there.

When I'll have to change the code and "re-integrate" the external sqlite DB in the previous project, I'll let you know if I'll encounter that issue again.
Thank you.
 

alon

Active Member
Licensed User
Longtime User

Do you know where can i find a sample or instruction , of how the migration for sql db is working.
Because i need to add new columns from time to time.
Thanks
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…