B4J Library [B4X] MiniORMUtils

MiniORMUtils
Version: 3.30

This library can be use for creating db schema and performing CRUD operations.
It is suitable for Web API Template or any database system.
Currently it supports SQLite and MySQL (B4J).

Visor


Project Template:
[B4X] [Project Template] MiniORM

Examples:
Create ConnectionInfo object
B4X:
Dim Info As ConnectionInfo
Info.Initialize
info.DBType = "SQLite"
Info.DBFile = "data.db"

Create ORMConnector object
B4X:
Dim Conn As ORMConnector
Conn.Initialize(Info)

Check if database exist
B4X:
Dim DBFound As Boolean = Conn.DBExist
If DBFound = False Then
    LogColor($"${Conn.DBType} database not found!"$, COLOR_RED)
    CreateDatabase
End If

Create database
B4X:
Private Sub CreateDatabase
    Dim Success As Boolean = Conn.DBCreate
    If Success = False Then
        Log("Database creation failed!")
        Return
    End If
    ' The rest of code for creating tables
End Sub

Initialize MiniORM object
B4X:
Dim DB As MiniORM
DB.Initialize(DBType, DBOpen)
DB.QueryAddToBatch = True

Create table
B4X:
DB.Table = "tbl_category"
DB.Columns.Add(DB.CreateColumn2(CreateMap("Name": "category_name")))
DB.Create

Insert rows
B4X:
DB.Columns = Array("category_name")
DB.Insert2(Array("Hardwares"))
DB.Insert2(Array("Toys"))

Execute NonQuery Batch
B4X:
Wait For (DB.ExecuteBatch) Complete (Success As Boolean)
If Success Then
    Log("Database is created successfully!")
Else
    Log("Database creation failed!")
End If
DB.Close

Select All Rows
B4X:
DB.Table = "tbl_category"
DB.Query
Dim Items As List = DB.Results

Update row
B4X:
DB.Table = "tbl_products"
DB.Columns = Array("category_id", "product_code", "product_name", "product_price")
DB.Id = 2
DB.Save2(Array(Category_Id, Product_Code, Product_Name, Product_Price))

More examples on GitHub README page
https://github.com/pyhoon/MiniORMUtils-B4X

 

Attachments

  • MiniORMUtils.b4xlib
    13.6 KB · Views: 118
Last edited:

aeric

Expert
Licensed User
Longtime User
Testing this version 3 on MiniORM B4X template but getting error when creating database using ExecuteBatch.
Not sure why it used to work but not now.
I will use a different approach to solve this issue instead of troubleshooting the root cause.
 

aeric

Expert
Licensed User
Longtime User
Testing this version 3 on MiniORM B4X template but getting error when creating database using ExecuteBatch.
Not sure why it used to work but not now.
I will use a different approach to solve this issue instead of troubleshooting the root cause.

Found the issue.
I can't use DBParameters straight away. This can cause an issue of pass by reference.

B4X:
Public Sub AddNonQueryToBatch
    Dim paramsize As Int = ParametersCount
    Dim Args(paramsize) As Object
    Dim i As Int
    For Each Param In DBParameters
        Args(i) = Param
        i = i + 1
    Next
    DBSQL.AddNonQueryToBatch(DBStatement, Args)
    'DBSQL.AddNonQueryToBatch(DBStatement, DBParameters) ' Wrong
End Sub
 

aeric

Expert
Licensed User
Longtime User
Have you ever working with SQLite BLOB in B4i, B4A or B4J?

B4i-Blob-JSON.png

Next version will support BLOB.
 

aeric

Expert
Licensed User
Longtime User
Version: 3.30
Size: 14KB

Another major update!

What's New

1. BLOB column is now supported
2. ColumnsType property is added as Map of column name (or alias) as key and column type as value.
3. Specifying the ColumnsType is highly recommended especially when working with BLOB. It will fail in B4A if attempt to convert BLOB to String.
4. Parameters variable is now array of Object. It is no longer a List in B4J nor array of String in B4A and B4i.
5. Most subs are affected by #4 and updated
6. Query sub updated
7. DBUtils and JSON libraries are no longer required for B4A and B4i. These libraries have been removed in manifest.txt
8. First property is not returning a default Map
9. Execute2 sub added
 
Top