Basic4Android and SQLite

D

Deleted member 103

Guest
Hi,

here is a small example with SQLite?


Ciao,
Filippo
 
D

Deleted member 103

Guest
Hallo Klaus,

ich glaube das Google meine Frage falsch übersetzt hat, ich wollte nur fragen ob jemanden ein Beispiel mit SQLite hat.

I think that Google has translated my question wrong, I just wanted to ask if someone has an example with SQLite.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here is a small program (don't forget to first add a reference to SQL library in the Libraries tab):
B4X:
Sub Process_Globals
    Dim SQL1 As SQL
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    If SQL1.IsInitialized = False Then
        SQL1.Initialize(File.DirDefaultExternal, "test1.db", True)
    End If
    sql1.ExecNonQuery("DROP TABLE IF EXISTS table1")
    SQL1.ExecNonQuery("CREATE TABLE table1 (col1 TEXT , col2 INTEGER, col3 INTEGER)")
    SQL1.ExecNonQuery2("INSERT INTO table1 VALUES (?, ?, 0)", Array As String("some text", 2))
    sql1.ExecNonQuery("INSERT INTO table1 VALUES ('2', 22, 'asd')")
    sql1.ExecNonQuery("INSERT INTO table1 VALUES (33, 211, '0')")
    
    SQL1.BeginTransaction
    Try
        'block of statements like
        For i = 1 To 10
            SQL1.ExecNonQuery("INSERT INTO table1 VALUES(1, 2, 3)")
        Next
        SQL1.TransactionSuccessful
    Catch
        Log(LastException.Message)
    End Try
    SQL1.EndTransaction
    
    Log(SQL1.ExecQuerySingleResult("SELECT count(*) FROM table1 WHERE col2 > 10"))
    
    Dim Cursor As Cursor
    Cursor = SQL1.ExecQuery("SELECT col1, col2 FROM table1")
    For i = 0 To Cursor.RowCount - 1
        Cursor.Position = i
        Log(Cursor.GetString("col1"))
        Log(Cursor.GetInt("col2"))
    Next
    Cursor.Close
End Sub
 
Upvote 0
D

Deleted member 103

Guest
Hi Erel,

many Dak to the SQLite example.

I now have this error message, what can I do?
 

Attachments

  • Zwischenablage-1.jpg
    Zwischenablage-1.jpg
    30.9 KB · Views: 621
Upvote 0

Beja

Expert
Licensed User
Longtime User
Hi Erel,
today I used the example above (AS IS) without modifying anything, and get the following error msg (snapshot attached.)
Note: I used the emulator and not a physical device, and I am using B4A 2.71, so may be the b4a version?

@Filippo:
your SQLile tutorial on youtube is great but unfortunately I don't understand Spanish.

thanks
 

Attachments

  • b4a-err.jpg
    b4a-err.jpg
    33.6 KB · Views: 332
Upvote 0

klaus

Expert
Licensed User
Longtime User
When you defined the Emulator, did you define a size for SD Card ?
You could also try to repace File.DirDefaultExternal by File.DirInternal.
You could also have a look at chapter 3 SQLite Database in the User's Guide.

Best regards.
 
Upvote 0
Top