Android Question SQL inititalize - howto?

TheRealMatze

Active Member
Licensed User
I try to get through the demos and the manuals, but it´s difficult. Most of the demos crashing on start, i don´t know if there is any java-issue - but in fact they are not running :(...
So i try to made it with the manuals - but this is also difficult. Example: sqlight

The manual describes on page 8 how to init
SQLite Databases Guide for Android, iOS, Java & Arduino / ESP8266 Programming (b4x.com)

So i try it in B4J

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private sql1 As SQL
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
   
    #if B4J
    sql1.Initializesqlite(File.DirData("test") ,"Test.db",True)
    #end if
   
    #if B4A
    sql1.Initialize(File.DirInternal("test"), "Test.db", True)
    #end if
   
    CreateTabIfNotExist("Test2","[Name] TEXT,[Ort] TEXT,[ID] INTEGER,PRIMARY KEY([ID] AUTOINCREMENT)")
    sql1.ExecNonQuery("Insert into test2(name,ort) values('hugo egon','bochum')")
   
    Dim resultset As ResultSet = sql1.ExecQuery("select * from test2 order by [name] asc")
    Do While resultset.NextRow
        Log(resultset.GetString("Name") & TAB & resultset.getstring("ort") & TAB & resultset.getint("id"))
    Loop
   
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

Sub CreateTabIfNotExist(TabName As String, TabDefinition As String)
    Dim resultSet As ResultSet = sql1.ExecQuery("select name from sqlite_master where type='table' and name='" & TabName & "'")
    If resultSet.NextRow=False Then
        Log("Tabelle " & TabName & " wird erzeugt..."):    sql1.ExecNonQuery("Create Table [" & TabName & "] (" & TabDefinition & ")")
    Else
        Log("Tabelle " & TabName & " gefunden!")
    End If
End Sub

Ok, i´m wondering why there is no .eof at the resultset, but when i finaly come to the sql-syntax everything is fine.
The Manual says the init is different between b4j and b4a / i. I copy/past it, but i got an exeption in b4a... in b4j it works like expected. i have no idea why.

Fehlerbeschreibung: Array erwartet.
Fehler in Zeile: 21
sql1.Initialize(File.DirInternal("test"), "Test.db", True)
Word: (

The lib in b4j called jSQL, in b4a only SQL. Is it the same?

Regards
Matze
 

TheRealMatze

Active Member
Licensed User
I see no option to withdraw my question - it´s already solved with try & error.
For b4a "sql1.Initialize(File.DirInternal,"Test.db",True)" is correct...
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
it´s already solved
It is still a mystery to me how you were able to get this code to work. When you create the definition of the table:
[ID] INTEGER,PRIMARY KEY you use upper case for ID and when you display the data:
resultset.getint("id") id is in lower case. The column names are case sensitive, so you should have gotten an error for this. The same thing with column name Ort and ort are not the same
 
Upvote 0

TheRealMatze

Active Member
Licensed User
You are absolutely right Mahares. In B4A the column-names are case-sensitive, in B4J not. Just because the initialize failed i did not reached this point. So i did not see this at the beginning and forgot to write it in my second post.

B4X:
#if b4j
    sql1.Initializesqlite(File.DirData("test") ,"Test.db",True)
#end if
#if b4a
    sql1.Initialize(File.DirInternal,"Test.db",True)
#End If

was the missing Part for me. But to clarify it - yes, to work under B4A the column-names must case-sensitive.
 
Upvote 0
Top