B4J Library [B4X] DBUtils 2

Mashiane

Expert
Licensed User
Longtime User
For your select statements, why not use ? and then put the m.get(key) as array of string()?
 

IslandMedic

Member
Licensed User
Longtime User
I added AUTOINCREMENT to the create table function. Sharing in case someone could use this.

B4X:
'Creates a new table with the given name.
'FieldsAndTypes - A map with the fields names as keys and the types as values.
'You can use the DB_... constants for the types.
'PrimaryKey - The column that will be the primary key. Pass empty string if not needed.
'AutoInc - The column that will need to be auto incrementing. Pass empty string if not needed.
Public Sub CreateTable(SQL As SQL, TableName As String, FieldsAndTypes As Map, PrimaryKey As String, AutoInc As String)
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("(")
    For i = 0 To FieldsAndTypes.Size - 1
        Dim field, ftype As String
        field = FieldsAndTypes.GetKeyAt(i)
        ftype = FieldsAndTypes.GetValueAt(i)
        If i > 0 Then sb.Append(", ")
        sb.Append(EscapeField(field)).Append(" ").Append(ftype)
        If field = PrimaryKey Then sb.Append(" PRIMARY KEY")
        If field = AutoInc Then sb.Append(" AUTOINCREMENT")
    Next
    sb.Append(")")
    Dim query As String
    query = "CREATE TABLE IF NOT EXISTS " & EscapeField(TableName) & " " & sb.ToString
    'Log("CreateTable: " & query)
    SQL.ExecNonQuery(query)
End Sub
 

LucaMs

Expert
Licensed User
Longtime User
Since it (perhaps ? ) served me, I "created" it as b4xlib (great effort, only inserted in a zip file with the addition of a simple manifest file ?).

If you agree, Erel, you could attach it to the first post and I will remove this post (indeed, you could remove it)
 

Attachments

  • B4XDBUtils.b4xlib
    4.5 KB · Views: 475

klaus

Expert
Licensed User
Longtime User
Hi Luca, unfortunately your manifest.txt file is not complete!
You have not added the RuntimePermissions library dependence for B4A.
You might also add the author:
Version=2.06
Author=Erel
B4J.DependsOn=jSQL
B4A.DependsOn=SQL,RuntimePermissions
B4i.DependsOn=iSQL
 

LucaMs

Expert
Licensed User
Longtime User
Uhm... and what is needed for B4J-B4I, about permissions (mainly B4i)? I don't know.

The author, in this case, is not so important ?

Thanks, Klaus.
 

LucaMs

Expert
Licensed User
Longtime User
Probably the best thing would be that I suggest Erel the methods I want to add and that he decides whether to add them or not.
Methods I wrote 6 years ago , so I have to check them first too.

What do you think about it?
 

klaus

Expert
Licensed User
Longtime User
Uhm... and what is needed for B4J-B4I, about permissions (mainly B4i)? I don't know.
In post #1, I saw this:
It depends on: SQL or jSQL or iSQL
B4A also depends on RuntimePermissions.

And I checked it.
The author, in this case, is not so important
For me it is!

It's up to Erel to decide.
 

LucaMs

Expert
Licensed User
Longtime User
B4X:
    Type tFieldInfo(FieldName As String, DataType As String, _
                            CanBeNull As Boolean, DefValue As Object)

' ----------------------------------------------------------------------
'
' Get all tables names as list
Public Sub GetTables(DB As SQL) As List
    Return ExecuteList(DB, "SELECT name FROM sqlite_master WHERE type = 'table'", Null, 0)
End Sub

' Gets informations about each fields in a table.
' Returns a list of tFieldInfo
Public Sub GetFieldsInfo(DB As SQL, TableName As String) As List

    Dim lstFieldsInfo As List
    lstFieldsInfo.Initialize
   
    Dim FieldsList As List
    FieldsList = ExecuteMemoryTable(DB, "PRAGMA table_info ('" & TableName & "')", Null, 0)
    Dim values() As String
    For I = 0 To FieldsList.Size - 1
        values = FieldsList.Get(I) 'FieldsList is a list of arrays

        Dim FieldInfo As tFieldInfo
        FieldInfo.Initialize
        FieldInfo.FieldName = values(1)
        FieldInfo.DataType = values(2)
        FieldInfo.CanBeNull = (values(3) = 1)
        FieldInfo.DefValue = values(4)

        lstFieldsInfo.Add(FieldInfo)
    Next

    Return lstFieldsInfo
End Sub

(This is my veeery old code; it should work, not tested now)
 

LucaMs

Expert
Licensed User
Longtime User
(This is my veeery old code; it should work, not tested now)
("old code": about 6 years ago!)

Now it seems that PRAGMA...

(from: https://www.sqlite.org/pragma.html)
Notes:
  1. Pragmas whose names are struck through are deprecated. Do not use them. They exist for historical compatibility.
  2. These pragmas are only available in builds using non-standard compile-time options.
  3. These pragmas are used for testing SQLite and are not recommended for use in application programs.
Uhm...?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…