B4J Library [SithasoDaisy] Plug n Play PHP CRUD REST API (MySQL, SQLite, MSServer, PostGreSQL) for BANano

Hi

Plug n Play? Yep...

1. Add the api.php file to your assets. Get it from this GitHub repo

I spoke about this in 2022 on the BVAD3 library.


2. Change the connection parameters in the api.php file.

1688823488498.png


Save the file and click 'Sync' in the IDE. You are good to go.

Example: We have a categories table in our db named bvad3products with column 'id' auto-increment.

1688824001906.png



Lets do some crud...
 

Attachments

  • SDUIFetch.bas
    7.4 KB · Views: 75
  • SDUIMySQLAPI.bas
    27.3 KB · Views: 75
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Create records

We initialize the class first! sdmysqlapi below is our sub-domain where our app is hosted on

B4X:
Dim mysqlapi As SDUIMySQLAPI
    mysqlapi.Initialize(Me, "mysqlapi", "https://localhost/sdmysqlapi/assets/api.php", "categories")
    mysqlapi.ShowLog = False
    mysqlapi.SchemaAddInt(Array("id"))
    mysqlapi.SchemaAddText(Array("name","color"))
    mysqlapi.PrimaryKey = "id"
    mysqlapi.AutoIncrement = "id"

B4X:
mysqlapi.PrepareRecord
    mysqlapi.SetField("name", "Anele Mbanga " & SDUIShared.DateNow)
    Dim id As String = BANano.Await(mysqlapi.CREATE)
    Log(id)

B4X:
    mysqlapi.PrepareRecord
    mysqlapi.SetField("id", 23)
    mysqlapi.SetField("name", "Cars " & SDUIShared.DateNow)
    Dim id As String = BANano.Await(mysqlapi.CREATE_OR_UPDATE)
    Log(mysqlapi.RowCount)
    Log(id)

You can also pass a map variable with

B4X:
mysqlapi.SetRecord(MapName)
 
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Update records

B4X:
mysqlapi.PrepareRecord
    mysqlapi.SetField("id", 67)
    mysqlapi.SetField("name", "Shandukani " & SDUIShared.DateNow)
    BANano.Await(mysqlapi.UPDATE)
    Log(mysqlapi.RowCount)

B4X:
mysqlapi.PrepareRecord
    mysqlapi.SetField("color", "green")
    BANano.Await(mysqlapi.UPDATE_BY("name", "Shandukani " & SDUIShared.DateNow))
 
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Read records

B4X:
Dim found As Map = BANano.Await(mysqlapi.READ(97))
    Log(found)

B4X:
    Dim found As Map = BANano.Await(mysqlapi.READ_BY("name", "Shandukani " & SDUIShared.DateNow))
    Log(found)

B4X:
mysqlapi.CLEAR_WHERE
    mysqlapi.ADD_WHERE("name", mysqlapi.OPERATOR_EQ, $"Cars ${SDUIShared.DateNow}"$)
    BANano.await(mysqlapi.SELECT_WHERE)
    Log(mysqlapi.result)


B4X:
mysqlapi.CLEAR_WHERE
    mysqlapi.ADD_ORDER_BY("name")
    BANano.await(mysqlapi.SELECT_ALL)
    Do While mysqlapi.NextRow
        Dim rec As Map = mysqlapi.Record
        'Log(rec)
    Loop

B4X:
mysqlapi.CLEAR_WHERE
mysqlapi.ADD_ORDER_BY_DESC("name")
    BANano.await(mysqlapi.SELECT_ALL)
    Do While mysqlapi.NextRow
        Dim rec As Map = mysqlapi.Record
        Log(rec)
    Loop
 
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Interesting reading...



 

yiankos1

Well-Known Member
Licensed User
Longtime User
I can't describe how helpfull was this one using sithasodaisy.

Can we add an authentication in order to make our calls safe. Because anyone who has the api link (https://localhost/sithasomultiuser/assets/api.php/records/posts/1) can get data.

I see at first post that there is SDUIFetch module, where there is Basic authentication method but i am not sure how to use it...

At github info page there are five types of authentication supported (API key, API key DB, Database, Basic, JWT). Can we have them?
 

Mashiane

Expert
Licensed User
Longtime User
SDUIFetch
This is just a wrapper to make using BANanoFetch easier to use and nothing else.

Can we add an authentication in order to make our calls safe
I am not a PHP expert sadly and I would also love to have such things working. Perhaps if someone could figure it out it would help.

Do you have any ideas perhaps? I had shared this in the forum because it was so easy plug and play.
 
Top