B4A Library ✨ Magic API Library: Build a Powerful Server-Side REST API with MySQL and File Management in Just 5 Minutes! 🚀

⚡️ Unleash the API Power: Magic API for Seamless B4X Integration



? Greetings, B4X enthusiasts! Today, I'm thrilled to unveil the magic behind our latest creation—the Magic API. Elevate your B4X applications with streamlined API integration, simplifying CRUD operations effortlessly. From uploading api.php to securing your connection data, this library is your key to seamless integration. Explore its advantages and fortify your data integrity with advanced security measures. Let's dive into the API revolution!



? Advantages of Magic API:

Unlock the Magic: Seamlessly integrate API functionality into your B4X applications.

Effortless CRUD Operations: Create, Read, Update, and Delete with ease, optimizing data management.

? Advanced Security Measures: API key authentication, HTTPS encryption, and input validation ensure data confidentiality and integrity.

? Base URL: http://example.com/api.php

? User-Friendly Initialization: Just upload, edit the header with your connection data, and you're ready to communicate!





?Usage Instructions:



1. Upload the api.php file to your server.

2. Edit the header with your connection data.

3. You're ready to communicate your app with your mobile and desktop applications.



Note: The API works fine without the library, but the library will help you communicate more easily.



? Explore API Documentation:

API Documentation

Security


The API employs several security measures to ensure the integrity and confidentiality of your data:



  • Authentication: All API requests require an API key to be included as a query parameter. This key uniquely identifies the client making the request and provides access control to the API endpoints.
  • HTTPS: Communication with the API is always encrypted over HTTPS, ensuring that data transmitted between the client and the server remains confidential.
  • Input validation: The API performs input validation and sanitization to prevent common security vulnerabilities such as SQL injection or cross-site scripting (XSS) attacks.
Base URL


Authentication

The API uses an API key for authentication. The API key must be included as a parameter in all requests.



Authentication Parameters

  • api_key (required): The API key to authenticate the request.
Available Resources



🌟 Ultimate Guide: Manage Any MySQL Database with DatabaseAPI and api.php in B4X




Welcome, developers! This guide shows you how to use the B4X DatabaseAPI class with the api.php script to interact with any MySQL database in a simple, secure, and efficient way. We provide practical examples for each method, both in B4X and direct API calls using cURL, along with instructions for configuring connection variables, security measures, and the advantages of using this API. 🚀





🛠️ Initial Setup



Server and api.php Configuration




Before uploading api.php to your server, edit the connection variables to match your MySQL environment:





php



// Database connection parameters

$servername = "localhost"; // Change to your MySQL server address (e.g., "mysql.yourserver.com")

$username = "root"; // Change to your database user

$password = ""; // Change to the user’s password

$dbname = "inventory_system"; // Default database

$apiKey = '1234'; // Change to a secure, unique API key







Steps to upload to the server:





  1. Open api.php in a text editor.
  2. Update $servername, $username, $password, and $apiKey to match your setup.
  3. Optionally, set a default database in $dbname if you don’t plan to use the dbname parameter in requests.
  4. Upload api.php to your web server’s root directory (e.g., /public_html/api.php or C:\xampp\htdocs\api.php for a local server).
  5. Ensure your server has PHP and MySQL enabled.


API Security



The API implements several security measures to protect your data:





  • API Key Authentication: All requests require an api_key parameter (e.g., 1234). Change $apiKey in api.php to a unique, secure value and store it safely.
  • SQL Injection Prevention: Uses prepared statements (mysqli::prepare) and data escaping (real_escape_string) to prevent SQL injections.
  • Query Restriction: Custom queries (custom_query) are restricted to SELECT statements to prevent unauthorized modifications.
  • CORS Configuration: Allows requests from any origin (Access-Control-Allow-Origin: *), but you can restrict it to specific domains for enhanced security (e.g., Access-Control-Allow-Origin: https://yourdomain.com).
  • UTF-8 Encoding: Ensures compatibility with special characters and avoids encoding issues.
  • No Caching: Headers like Cache-Control and Expires prevent responses from being cached, reducing risks of accessing outdated data.


Security Recommendations:





  • Use a strong, unique API key (e.g., a generated hash).
  • Enable HTTPS on your server to encrypt communications.
  • Restrict MySQL user permissions to the minimum required (e.g., read/write only for the specific database).
  • Consider adding a firewall or .htaccess rules to limit access to api.php.


Advantages of Using the API



  • Cross-Platform Compatibility: As a RESTful API, api.php can be used from any platform or language (B4X, JavaScript, Python, etc.) via HTTP requests, making it ideal for mobile, web, or desktop applications.
  • Database Flexibility: Connect to any MySQL database by specifying the dbname parameter in the URL, without modifying server code.
  • Scalability: Supports complex operations like batch inserts, table joins, and custom queries, suitable for large-scale projects.
  • Easy Integration: The DatabaseAPI class simplifies interactions in B4X, while standard HTTP calls allow integration with other technologies.
  • Robust Security: Includes authentication, SQL injection prevention, and HTTPS support for secure operations.
  • Simple Maintenance: The modular design of api.php and DatabaseAPI makes updates and customizations straightforward.


Sample Database



For the examples, we use a test database called inventory_system. Create the tables with this SQL (in phpMyAdmin or MySQL console):





' Example:
CREATE DATABASE inventory_system;

USE inventory_system;



CREATE TABLE products (

id INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(255),

price DECIMAL(10,2),

stock INT,

category_id INT

);



CREATE TABLE categories (

id INT AUTO_INCREMENT PRIMARY KEY,

category_name VARCHAR(255)

);



-- Sample data

INSERT INTO products (name, price, stock, category_id) VALUES

('Laptop', 999.99, 10, 1),

('Mouse', 29.99, 50, 2),

('Keyboard', 59.99, 20, 2);



INSERT INTO categories (category_name) VALUES

('Electronics'),

('Accessories');



Note: You can use any database by specifying dbname in requests (e.g., ?dbname=my_database).



B4X Configuration



Initialize the DatabaseAPI class with your database name in the URL:





' Example:
Sub Process_Globals

Private db As DatabaseAPI

End Sub



Sub AppStart

db.Initialize(Me, "DB", "http://localhost/api.php?dbname=inventory_system", "YOUR APIKEY")

End Sub





Requirements



  • Server: Ensure api.php is in the server’s root directory (e.g., C:\xampp\htdocs\api.php for XAMPP).
  • API Key: Use 1234 or the key defined in api.php.
  • Testing: Use Postman, cURL, or a terminal to test direct API calls.





📋 Usage Examples



Direct API Calls (cURL)


🌐 API CRUD – Full cURL Examples​


🔹 Insert Data​


' Insert a single product:
 POST "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products" 
H "Content-Type: application/json" 
d "{"name":"Monitor","price":199.99,"stock":15,"category_id":1}"


' Insert multiple products (separate calls):
POST "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products" 
H "Content-Type: application/json" 
d "{"name":"Headphones","price":79.99,"stock":30,"category_id":2}"


POST "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products" 
H "Content-Type: application/json" 
d "{"name":"Webcam","price":49.99,"stock":25,"category_id":2}"


' Batch insert (multiple rows in one request):
POST "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&action=batch_insert" 
H "Content-Type: application/json" 
d "[{"name":"Tablet","price":299.99,"stock":8,"category_id":1},
{"name":"Speaker","price":89.99,"stock":12,"category_id":2}]"


' Insert or update product by column value (UPSERT):
' First check
GET "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&column=name&value=Mouse"


'Then insert or update
PUT "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&column=name&value=Mouse" 
H "Content-Type: application/json" 
d "{"name":"Mouse","price":34.99,"stock":60,"category_id":2}"




🔹 Delete Data​


' Delete by ID:
DELETE "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&id=1"


' Delete by column (name = Mouse):
DELETE "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&column=name&value=Mouse"


' Delete by column with comparison (price > 100):
DELETE "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&column=price&value=100&comparison=>"


' Clear entire table:
DELETE "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&action=clear_table"


' Delete a table:
DELETE "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=new_table&action=drop_table"


' Delete a column:
DELETE "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&action=drop_column&column=status"




🔹 Update Data​


' Update by ID:
PUT "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&id=2" 
H "Content-Type: application/json" 
d "{"name":"Mouse","price":39.99,"stock":45,"category_id":2}"


' Update by column (name = Keyboard):
PUT "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&column=name&value=Keyboard" 
H "Content-Type: application/json" 
d "{"stock":25}"


' Update with comparison (price > 50):
PUT "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&column=price&value=50&comparison=>" 
H "Content-Type: application/json" 
d "{"stock":30}"


' Update multiple rows (all category_id = 2):
PUT "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&action=update_multiple&column=category_id&value=2" 
H "Content-Type: application/json" 
d "{"stock":100}"


' Update two tables with JOIN:
PUT "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&table2=categories&join_column1=category_id&join_column2=id&condition_column=category_id&condition_value=2&comparison==" 
H "Content-Type: application/json" 
d "{"table1":{"stock":50},"table2":{"category_name":"Updated Accessories"}}"




🔹 Search / Select Data​


' Get by ID:
GET "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&id=2"


' Get all rows:
GET "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products"


' Search by column (category_id = 1):
GET "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&column=category_id&value=1"


' Search with comparison (price > 100):
GET "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&column=price&value=100&comparison=>"


' Join two tables (products + categories):
GET "http://localhost/api.php?api_key=1234&dbname=inventory_system&action=custom_query&query=SELECT%20*%20FROM%20products%20INNER%20JOIN%20categories%20ON%20products.category_id%20=%20categories.id"


' Join with filter (price > 100):
GET "http://localhost/api.php?api_key=1234&dbname=inventory_system&action=custom_query&query=SELECT%20*%20FROM%20products%20INNER%20JOIN%20categories%20ON%20products.category_id%20=%20categories.id%20WHERE%20products.price%20%3E%20100"


' Custom query (name and price only):
GET "http://localhost/api.php?api_key=1234&dbname=inventory_system&action=custom_query&query=SELECT%20name%2C%20price%20FROM%20products%20WHERE%20price%20%3E%20100"




🔹 Table and Column Operations​


' Create a new table:
POST "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=new_table&action=create_table" 
H "Content-Type: application/json" 
d "{"name":"VARCHAR(255)","price":"DECIMAL(10,2)","stock":"INT","category_id":"INT"}"


' Create a new column:
PATCH "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&column=status&type=VARCHAR(50)"


' Get table schema:
GET "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&action=schema"


' Check if table exists:
GET "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&action=exists_table"




🔹 Statistics and Aggregations​


' Sum column (total price):
GET "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&action=sum&column=price"


' Count all rows:
GET "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&action=count"


' Count specific values (category_id = 2):
GET "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&action=count_value&column=category_id&value=2"


' Get percentage (category_id = 2):
GET "http://localhost/api.php?api_key=1234&dbname=inventory_system&table=products&action=percentage&column=category_id&value=2"


B4X Code: Complete Examples



'Full Example:
' In Main module or another module

Sub Process_Globals

Private db As DatabaseAPI

End Sub



Sub AppStart

' Initialize with your database name

db.Initialize(Me, "DB", "http://localhost/api.php?dbname=inventory_system", "1234")



' Insertmaps: Insert a single product

Dim data As Map

data.Initialize

data.Put("name", "Monitor")

data.Put("price", 199.99)

data.Put("stock", 15)

data.Put("category_id", 1)

db.Insertmaps(data, "products")



' InsertMultipleMaps: Insert multiple products individually

Dim ListOfMaps As List

ListOfMaps.Initialize

Dim m1, m2 As Map

m1.Initialize

m1.Put("name", "Headphones")

m1.Put("price", 79.99)

m1.Put("stock", 30)

m1.Put("category_id", 2)

m2.Initialize

m2.Put("name", "Webcam")

m2.Put("price", 49.99)

m2.Put("stock", 25)

m2.Put("category_id", 2)

ListOfMaps.Add(m1)

ListOfMaps.Add(m2)

db.InsertMultipleMaps(ListOfMaps, "products")



' BatchInsert: Insert multiple products in one call

db.BatchInsert(ListOfMaps, "products")



' InsertOrUpdateMapForColumnValue: Insert or update based on name

Dim updateData As Map

updateData.Initialize

updateData.Put("name", "Mouse")

updateData.Put("price", 34.99)

updateData.Put("stock", 60)

updateData.Put("category_id", 2)

db.InsertOrUpdateMapForColumnValue("products", "name", "Mouse", updateData)



' Delete: Delete a product by ID

db.Delete("products", "1")



' DeleteByColumn: Delete products by name

db.DeleteByColumn("products", "name", "Mouse")



' DeleteByColumn_comparison: Delete products with price > 100

db.DeleteByColumn_comparison("products", "price", "100", ">")



' Update: Update a product by ID

Dim updateMap As Map

updateMap.Initialize

updateMap.Put("name", "Mouse")

updateMap.Put("price", 39.99)

updateMap.Put("stock", 45)

updateMap.Put("category_id", 2)

db.Update("products", "2", updateMap)



' UpdateByColumn: Update stock by name

Dim updateByColumnMap As Map

updateByColumnMap.Initialize

updateByColumnMap.Put("stock", 25)

db.UpdateByColumn("products", "name", "Keyboard", updateByColumnMap)



' UpdateByColumn_comparison: Update stock for expensive products

Dim updateByColumnCompMap As Map

updateByColumnCompMap.Initialize

updateByColumnCompMap.Put("stock", 30)

db.UpdateByColumn_comparison("products", "price", "50", ">", updateByColumnCompMap)



' UpdateMultipleRows: Update stock for category

Dim updateMultiMap As Map

updateMultiMap.Initialize

updateMultiMap.Put("stock", 100)

db.UpdateMultipleRows("products", "category_id", "2", "=", updateMultiMap)



' UpdateTwoTables: Update products and categories

Dim updateTwoTablesMap As Map

updateTwoTablesMap.Initialize

updateTwoTablesMap.Put("table1", CreateMap("stock": 50))

updateTwoTablesMap.Put("table2", CreateMap("category_name": "Updated Accessories"))

db.UpdateTwoTables("products", "categories", "category_id", "id", "category_id", "2", "=", updateTwoTablesMap)



' SearchforId: Find product by ID

db.SearchforId("products", "2")



' GetTable: Retrieve all products

db.GetTable("products")



' Search: Find products by category

db.Search("products", "category_id", "1")



' Search_comparison: Find expensive products

db.Search_comparison("products", "price", "100", ">")



' ClearTable: Clear all products

db.ClearTable("products")



' SumColumn: Sum product prices

db.SumColumn("products", "price")



' CountRows: Count all products

db.CountRows("products")



' CountValue: Count products in category

db.CountValue("products", "category_id", "2")



' CreateTable: Create a new table

Dim columns As Map

columns.Initialize

columns.Put("name", "VARCHAR(255)")

columns.Put("price", "DECIMAL(10,2)")

columns.Put("stock", "INT")

columns.Put("category_id", "INT")

db.CreateTable("new_table", columns)



' CreateColumn: Add a status column

db.CreateColumn("products", "status", "VARCHAR(50)")



' DeleteColumn: Remove status column

db.DeleteColumn("products", "status")



' CustomQuery: Custom SELECT query

db.CustomQuery("SELECT name, price FROM products WHERE price > 100")



' DeleteTable: Drop a table

db.DeleteTable("new_table")



' JoinTables: Join products and categories

db.JoinTables("products", "categories", "INNER", "category_id", "id")



' FilterJoinTables: Join with filter

db.FilterJoinTables("products", "categories", "INNER", "category_id", "id", "products.price", "100", ">")



' GetTableSchema: Get product table schema

db.GetTableSchema("products")



' ExistsTable: Check if table exists

db.ExistsTable("products")



' GetPercentage: Get percentage of products in category

db.GetPercentage("products", "category_id", "2", "=")

End Sub



' Callbacks for handling responses

Private Sub DB_Insertmaps(Result As Map, Success As Boolean)

If Success Then Log($"Inserted: ${Result}"$) Else Log("Insert failed")

End Sub



Private Sub DB_BatchInsert(InsertedIds As List, Success As Boolean)

If Success Then Log($"Batch inserted IDs: ${InsertedIds}"$) Else Log("Batch insert failed")

End Sub



Private Sub DB_InsertOrUpdateMapForColumnValue(Action As String, Success As Boolean)

If Success Then Log($"Action: ${Action}"$) Else Log("Insert/Update failed")

End Sub



Private Sub DB_Delete(TableName As String, Success As Boolean)

If Success Then Log($"Deleted from: ${TableName}"$) Else Log("Delete failed")

End Sub



Private Sub DB_DeleteByColumn(TableName As String, Success As Boolean)

If Success Then Log($"Deleted by column from: ${TableName}"$) Else Log("Delete by column failed")

End Sub



Private Sub DB_Update(TableName As String, Success As Boolean)

If Success Then Log($"Updated: ${TableName}"$) Else Log("Update failed")

End Sub



Private Sub DB_UpdateByColumn(TableName As String, Success As Boolean)

If Success Then Log($"Updated by column: ${TableName}"$) Else Log("Update by column failed")

End Sub



Private Sub DB_UpdateMultipleRows(TableName As String, Success As Boolean)

If Success Then Log($"Multiple rows updated: ${TableName}"$) Else Log("Update multiple rows failed")

End Sub



Private Sub DB_UpdateTwoTables(Result As Map, Success As Boolean)

If Success Then Log($"Two tables updated: ${Result}"$) Else Log("Update two tables failed")

End Sub



Private Sub DB_SearchforId(Result As Map, Success As Boolean)

If Success Then Log($"Found: ${Result}"$) Else Log("Search by ID failed")

End Sub



Private Sub DB_GetTable(Result As List, Success As Boolean)

If Success Then

For Each row As Map In Result

Log($"Row: ${row}"$)

Next

Else

Log("Get table failed")

End If

End Sub



Private Sub DB_Search(Result As List, Success As Boolean)

If Success Then

For Each row As Map In Result

Log($"Search result: ${row}"$)

Next

Else

Log("Search failed")

End If

End Sub



Private Sub DB_ClearTable(TableName As String, Success As Boolean)

If Success Then Log($"Table cleared: ${TableName}"$) Else Log("Clear table failed")

End Sub



Private Sub DB_SumColumn(Total As Double, Success As Boolean)

If Success Then Log($"Total: ${Total}"$) Else Log("Sum column failed")

End Sub



Private Sub DB_CountRows(Count As Int, Success As Boolean)

If Success Then Log($"Row count: ${Count}"$) Else Log("Count rows failed")

End Sub



Private Sub DB_CountValue(Count As Int, Success As Boolean)

If Success Then Log($"Value count: ${Count}"$) Else Log("Count value failed")

End Sub



Private Sub DB_CreateTable(TableName As String, Success As Boolean)

If Success Then Log($"Table created: ${TableName}"$) Else Log("Create table failed")

End Sub



Private Sub DB_CreateColumn(TableName As String, Success As Boolean)

If Success Then Log($"Column added to: ${TableName}"$) Else Log("Create column failed")

End Sub



Private Sub DB_DeleteColumn(TableName As String, Success As Boolean)

If Success Then Log($"Column deleted from: ${TableName}"$) Else Log("Delete column failed")

End Sub



Private Sub DB_CustomQuery(Result As List, Success As Boolean)

If Success Then

For Each row As Map In Result

Log($"Query result: ${row}"$)

Next

Else

Log("Custom query failed")

End If

End Sub



Private Sub DB_DeleteTable(TableName As String, Success As Boolean)

If Success Then Log($"Table dropped: ${TableName}"$) Else Log("Delete table failed")

End Sub



Private Sub DB_JoinTables(Result As List, Success As Boolean)

If Success Then

For Each row As Map In Result

Log($"Join result: ${row}"$)

Next

Else

Log("Join tables failed")

End If

End Sub



Private Sub DB_FilterJoinTables(Result As List, Success As Boolean)

If Success Then

For Each row As Map In Result

Log($"Filtered join result: ${row}"$)

Next

Else

Log("Filter join tables failed")

End If

End Sub



Private Sub DB_GetTableSchema(Result As List, Success As Boolean)

If Success Then

For Each col As Map In Result

Log($"Column: ${col}"$)

Next

Else

Log("Get table schema failed")

End If

End Sub



Private Sub DB_ExistsTable(Exists As Boolean, Success As Boolean)

If Success Then Log($"Table exists: ${Exists}"$) Else Log("Exists table failed")

End Sub



Private Sub DB_GetPercentage(Percentage As Double, Success As Boolean)

If Success Then Log($"Percentage: ${Percentage}"$) Else Log("Get percentage failed")

End Sub








🎉 Implementation Tips



  • Flexibility: Use the dbname parameter in the URL to connect to any database (e.g., http://localhost/api.php?dbname=my_database).
  • Testing: Test cURL calls with Postman or a terminal. In B4X, include DatabaseAPI.bas in your project.
  • Errors: Ensure XAMPP is running, api.php is in htdocs, and the database exists.
  • Security: Use a strong API key, enable HTTPS, and restrict MySQL permissions for enhanced protection.
  • Customization: Adjust the URL and API key to match your environment.




## Methods



Insertmaps(maps As Map, tablename As String)



Performs an insertion operation on the specified table of the API.

Example usage:
Dim data As Map

data.Initialize

data.Put("Column1", "Value1")

data.Put("Column2", "Value2")

MagicApi.Insertmaps(data, "table")



Delete(tablename As String, id As String)
Deletes a record from the specified table of the API using the ID.

Example usage:
MagicApi.Delete("table", "12")



DeleteByColumn(tablename As String, column As String, value As String)
Deletes records from the specified table of the API using a column and a value.

Example usage:
MagicApi.DeleteByColumn("table", "column", "value")



DeleteByColumn_comparison(tablename As String, column As String, value As String,comparison As String)
Deletes records from the specified table of the API using a column and a value and comparison <,>,<=,>=.

Example usage:
MagicApi.DeleteByColumn_comparison("table", "age", "20",">")



Update(tablename As String, id As String, data As Map)
Updates a record in the specified table of the API using the ID and the provided data.

Example usage:
Dim data As Map

data.Initialize

data.Put("Column1", "NewValue1")

data.Put("Column2", "NewValue2")

MagicApi.Update("table", "12", data)



UpdateByColumn(tablename As String, column As String, value As String, data As Map)
Updates records in the specified table of the API using a column, a value, and the provided data.

Example usage:
Dim data As Map

data.Initialize

data.Put("Column1", "NewValue1")

data.Put("Column2", "NewValue2")

MagicApi.UpdateByColumn("table", "column", "value", data)



UpdateByColumn_comparison(tablename As String, column As String, value As String,comparison As String, data As Map)
Updates records in the specified table of the API using a column, a value, and the provided data and comparison <,>,>=,<=.

Example usage:
Dim data As Map

data.Initialize

data.Put("Column1", "NewValue1")

data.Put("Column2", "NewValue2")

MagicApi.UpdateByColumn_comparison("table", "age", "15",">=", data)



SearchforId(tablename As String, id As String)
Searches for a record in the specified table of the API using the ID.

Example usage:
'

MagicApi.SearchforId("table", "123")

wait for eventname_SearchforId(m As Map, success As Boolean)

if success = true then

m.get("Column 1")

m.get("Column 2")

else



end if



GetTable(tablename As String)
Gets all records from the specified table of the API.

' Example usage:
MagicApi.GetTable("table")

Wait For eventname_GetTable(x As List, success As Boolean)

    For Each col As Map In x

        col.Get("Column name1")

        col.Get("Column name 2")

    Next



Search(tablename As String, column As String, value As String)
Performs a search in the specified table of the API using a column and a value.

' Example usage:
MagicApi.Search("table", "column", "value")

wait for EventName_Search(x as list, success as Boolean)

if success = true then

 For Each col As Map In x

col.Get("Column name1")

col.Get("Column name 2")

next

else



end if



sub





Search_comparison(tablename As String, column As String, value As String,comparison As String)
Performs a search in the specified table of the API using a column and a value and comparison.

' Example usage:
MagicApi.Search_comparison("table", "age", "35","<=")

wait for EventName_Search(x as list, success as Boolean)

if success = true then

 For Each col As Map In x

col.Get("Column name1")

col.Get("Column name 2")

next

else



end if



sub





Complete Example


example:
Sub Process_Globals

    Private magicApi As MagicApi

End Sub



Sub Globals

    ' ...

End Sub



Sub Activity_Create(FirstTime As Boolean)

    ' ...

    magicApi.Initialize(Me, "MyEventName", "http://example.com")

End Sub



' Implement the events generated by the library

Sub MyEventName_Insertmaps(m As Map, success As Boolean)

    ' ...

End Sub



Sub MyEventName_Delete(tablename As String, success As Boolean)

    ' ...

End Sub



' Implement other generated events...



Sub SomeSub

    ' Examples of using the library methods

    Dim data As Map

    data.Initialize

    data.Put("Column1", "Value1")

    data.Put("Column2", "Value2")

    magicApi.Insertmaps(data, "table")



    magicApi.Delete("table", "123")



    magicApi.DeleteByColumn("table", "column", "value")



    Dim updateData As Map

    updateData.Initialize

    updateData.Put("Column1", "NewValue1")

    updateData.Put("Column2", "NewValue2")

    magicApi.Update("table", "123", updateData)



    magicApi.UpdateByColumn("table", "column", "value", updateData)



    magicApi.SearchforId("table", "123")



    magicApi.GetTable("table")



    magicApi.Search("table", "column", "value")

End Sub





MagicAPI Module - New Routines
1. InsertOrUpdateMapForColumnValue
Description:

This routine checks whether a record exists in the specified table by searching for a column value. If the record exists, it updates the record; if not, it inserts a new record.



Parameters:



tablename (String): The name of the table where the operation will take place.
searchColumn (String): The column used for the search.
searchValue (String): The value to look for in the searchColumn.
data (Map): The data to insert or update.
Event Triggered:

<EventName>_InsertOrUpdateMapForColumnValue(message As String, success As Boolean)



message can be "Insert", "Update", or "Error".
success indicates if the operation was successful.
Example 1: Insert or Update a User Record



' Example usage - Insert or Update:
Dim userData As Map

userData.Initialize

userData.Put("name", "John Doe")

userData.Put("email", "[email protected]")

userData.Put("age", 30)



MagicAPI.InsertOrUpdateMapForColumnValue("users", "email", "[email protected]", userData)



Wait For MagicAPI_InsertOrUpdateMapForColumnValue(message As String, success As Boolean)



If success Then

Select Case message

Case "Insert" Log("A new user record was inserted.")

Case "Update" Log("The existing user record was updated.")

End Select

Else Log("Error: Unable to perform the operation.")

End If



Example 2: Insert or Update a Product Record



' Example usage - Insert or Update Product:
Dim productData As Map

productData.Initialize

productData.Put("name", "Wireless Mouse")

productData.Put("price", 29.99)

productData.Put("stock", 100)



MagicAPI.InsertOrUpdateMapForColumnValue("products", "name", "Wireless Mouse", productData)



Wait For MagicAPI_InsertOrUpdateMapForColumnValue(message As String, success As Boolean)



If success Then

Select Case message

Case "Insert"

Log("A new product record was inserted.")

Case "Update"

Log("The existing product record was updated.")

End Select

Else Log("Error: Unable to perform the operation.")

End If



2. InsertMultipleMaps
Description:

This routine inserts multiple records into the specified table using a list of maps, where each map represents a record.



Parameters:



ListOfMaps (List): A list of maps containing the data to insert. Each map represents one record.
TableName (String): The name of the table where the records will be inserted.
Event Triggered:

<EventName>_Insertmaps(m As Map, success As Boolean)



m: The map representing the data that was inserted.
success: Indicates if the operation was successful.
Example 1: Insert Multiple User Records



' Example usage - Insert Multiple Users:
Dim userRecords As List

userRecords.Initialize



Dim user1 As Map

user1.Initialize

user1.Put("name", "Alice")

user1.Put("email", "[email protected]")

user1.Put("age", 25)



Dim user2 As Map

user2.Initialize

user2.Put("name", "Bob")

user2.Put("email", "[email protected]")

user2.Put("age", 28)



userRecords.AddAll(Array(user1, user2))



MagicAPI.InsertMultipleMaps(userRecords, "users")



Wait For MagicAPI_Insertmaps(m As Map, success As Boolean)



If success Then

Log($"Record inserted: ${m}"$)

Else Log("Error inserting record.")

End If



Example 2: Insert Multiple Product Records



' Example usage - Insert Multiple Products:
Dim productRecords As List

productRecords.Initialize



Dim product1 As Map

product1.Initialize

product1.Put("name", "Keyboard")

product1.Put("price", 49.99)

product1.Put("stock", 50)



Dim product2 As Map

product2.Initialize

product2.Put("name", "Monitor")

product2.Put("price", 199.99)

product2.Put("stock", 20)



productRecords.AddAll(Array(product1, product2))



MagicAPI.InsertMultipleMaps(productRecords, "products")



Wait For MagicAPI_Insertmaps(m As Map, success As Boolean)



If success Then

Log($"Record inserted: ${m}"$)

Else

Log("Error inserting record.")

End If



Notes
Both routines provide asynchronous operations, and results are returned via their respective events.
Error handling is embedded, and failed operations trigger the appropriate event with a success = False.
Ensure the data maps are properly initialized and contain valid column names and values for the target table.


MagicApi library
# B4X Library Documentation

## Description
This library provides methods for performing CRUD (Create, Read, Update, Delete) operations on an API. It is designed to interact with an API that uses JSON as the data exchange format.

## Installation
1. Download the MagicApi.b4xlib library file.
2. Copy the MagicApi.b4xlib file to the additional libraries folder in your B4X development environment.

## Initialization
Before using the library methods, you need to initialize it by calling the `Initialize` method and providing the following parameters:
- `CallbackModule` (Object): the name of the callback module where the events handling API responses are located.
- `cEventname` (String): the base name for the response events.
- `urlbase` (String): the base URL of the API.
- `api_key` (String): The api key defined in the php file.

## Events
The library generates response events for each CRUD operation. These events should be implemented in the specified callback module during initialization.

### Generated Events
- `EventName_Insertmaps(m As Map, success As Boolean)`
- `EventName_Delete(tablename As String, success As Boolean)`
- `EventName_DeleteByColumn(tablename As String, success As Boolean)`
- `EventName_Update(tablename As String, success As Boolean)`
- `EventName_UpdateByColumn(tablename As String, success As Boolean)`
- `EventName_SearchforId(m As Map, success As Boolean)`
- `EventName_GetTable(x As List, success As Boolean)`
- `EventName_Search(x As List, success As Boolean)`

## Methods

Insertmaps(maps As Map, tablename As String)


Performs an insertion operation on the specified table of the API.
Example usage:
Dim data As Map
data.Initialize
data.Put("Column1", "Value1")
data.Put("Column2", "Value2")
MagicApi.Insertmaps(data, "table")

Delete(tablename As String, id As String)​

Deletes a record from the specified table of the API using the ID.
Example usage:
MagicApi.Delete("table", "12")

DeleteByColumn(tablename As String, column As String, value As String)​

Deletes records from the specified table of the API using a column and a value.
Example usage:
MagicApi.DeleteByColumn("table", "column", "value")

DeleteByColumn_comparison(tablename As String, column As String, value As String,comparison As String)​

Deletes records from the specified table of the API using a column and a value and comparison <,>,<=,>=.
Example usage:
MagicApi.DeleteByColumn_comparison("table", "age", "20",">")

Update(tablename As String, id As String, data As Map)​

Updates a record in the specified table of the API using the ID and the provided data.
Example usage:
Dim data As Map
data.Initialize
data.Put("Column1", "NewValue1")
data.Put("Column2", "NewValue2")
MagicApi.Update("table", "12", data)

UpdateByColumn(tablename As String, column As String, value As String, data As Map)​

Updates records in the specified table of the API using a column, a value, and the provided data.
Example usage:
Dim data As Map
data.Initialize
data.Put("Column1", "NewValue1")
data.Put("Column2", "NewValue2")
MagicApi.UpdateByColumn("table", "column", "value", data)

UpdateByColumn_comparison(tablename As String, column As String, value As String,comparison As String, data As Map)​

Updates records in the specified table of the API using a column, a value, and the provided data and comparison <,>,>=,<=.
Example usage:
Dim data As Map
data.Initialize
data.Put("Column1", "NewValue1")
data.Put("Column2", "NewValue2")
MagicApi.UpdateByColumn_comparison("table", "age", "15",">=", data)

SearchforId(tablename As String, id As String)​

Searches for a record in the specified table of the API using the ID.
Example usage:
'
MagicApi.SearchforId("table", "123")
wait for eventname_SearchforId(m As Map, success As Boolean)
if success = true then
m.get("Column 1")
m.get("Column 2")
else

end if

GetTable(tablename As String)​

Gets all records from the specified table of the API.
' Example usage:
MagicApi.GetTable("table")
Wait For eventname_GetTable(x As List, success As Boolean)
    For Each col As Map In x
        col.Get("Column name1")
        col.Get("Column name 2")
    Next

Search(tablename As String, column As String, value As String)​

Performs a search in the specified table of the API using a column and a value.
' Example usage:
MagicApi.Search("table", "column", "value")
wait for EventName_Search(x as list, success as Boolean)
if success = true then
 For Each col As Map In x
col.Get("Column name1")
col.Get("Column name 2")
next
else

end if

sub


Search_comparison(tablename As String, column As String, value As String,comparison As String)​

Performs a search in the specified table of the API using a column and a value and comparison.
' Example usage:
MagicApi.Search_comparison("table", "age", "35","<=")
wait for EventName_Search(x as list, success as Boolean)
if success = true then
 For Each col As Map In x
col.Get("Column name1")
col.Get("Column name 2")
next
else

end if

sub


Complete Example​


example:
Sub Process_Globals
    Private magicApi As MagicApi
End Sub

Sub Globals
    ' ...
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ' ...
    magicApi.Initialize(Me, "MyEventName", "http://example.com")
End Sub

' Implement the events generated by the library
Sub MyEventName_Insertmaps(m As Map, success As Boolean)
    ' ...
End Sub

Sub MyEventName_Delete(tablename As String, success As Boolean)
    ' ...
End Sub

' Implement other generated events...

Sub SomeSub
    ' Examples of using the library methods
    Dim data As Map
    data.Initialize
    data.Put("Column1", "Value1")
    data.Put("Column2", "Value2")
    magicApi.Insertmaps(data, "table")

    magicApi.Delete("table", "123")

    magicApi.DeleteByColumn("table", "column", "value")

    Dim updateData As Map
    updateData.Initialize
    updateData.Put("Column1", "NewValue1")
    updateData.Put("Column2", "NewValue2")
    magicApi.Update("table", "123", updateData)

    magicApi.UpdateByColumn("table", "column", "value", updateData)

    magicApi.SearchforId("table", "123")

    magicApi.GetTable("table")

    magicApi.Search("table", "column", "value")
End Sub


MagicAPI Module - New Routines

1. InsertOrUpdateMapForColumnValue

Description:
This routine checks whether a record exists in the specified table by searching for a column value. If the record exists, it updates the record; if not, it inserts a new record.

Parameters:

  • tablename (String): The name of the table where the operation will take place.
  • searchColumn (String): The column used for the search.
  • searchValue (String): The value to look for in the searchColumn.
  • data (Map): The data to insert or update.
Event Triggered:
<EventName>_InsertOrUpdateMapForColumnValue(message As String, success As Boolean)

  • message can be "Insert", "Update", or "Error".
  • success indicates if the operation was successful.

Example 1: Insert or Update a User Record

' Example usage - Insert or Update:
Dim userData As Map
userData.Initialize
userData.Put("name", "John Doe")
userData.Put("email", "[email protected]")
userData.Put("age", 30)

MagicAPI.InsertOrUpdateMapForColumnValue("users", "email", "[email protected]", userData)

Wait For MagicAPI_InsertOrUpdateMapForColumnValue(message As String, success As Boolean)

If success Then
Select Case message
Case "Insert" Log("A new user record was inserted.")
Case "Update" Log("The existing user record was updated.")
End Select
Else Log("Error: Unable to perform the operation.")
End If


Example 2: Insert or Update a Product Record

' Example usage - Insert or Update Product:
Dim productData As Map
productData.Initialize
productData.Put("name", "Wireless Mouse")
productData.Put("price", 29.99)
productData.Put("stock", 100)

MagicAPI.InsertOrUpdateMapForColumnValue("products", "name", "Wireless Mouse", productData)

Wait For MagicAPI_InsertOrUpdateMapForColumnValue(message As String, success As Boolean)

If success Then
Select Case message
Case "Insert"
Log("A new product record was inserted.")
Case "Update"
Log("The existing product record was updated.")
End Select
Else Log("Error: Unable to perform the operation.")
End If


2. InsertMultipleMaps

Description:
This routine inserts multiple records into the specified table using a list of maps, where each map represents a record.

Parameters:

  • ListOfMaps (List): A list of maps containing the data to insert. Each map represents one record.
  • TableName (String): The name of the table where the records will be inserted.
Event Triggered:
<EventName>_Insertmaps(m As Map, success As Boolean)

  • m: The map representing the data that was inserted.
  • success: Indicates if the operation was successful.

Example 1: Insert Multiple User Records

' Example usage - Insert Multiple Users:
Dim userRecords As List
userRecords.Initialize

Dim user1 As Map
user1.Initialize
user1.Put("name", "Alice")
user1.Put("email", "[email protected]")
user1.Put("age", 25)

Dim user2 As Map
user2.Initialize
user2.Put("name", "Bob")
user2.Put("email", "[email protected]")
user2.Put("age", 28)

userRecords.AddAll(Array(user1, user2))

MagicAPI.InsertMultipleMaps(userRecords, "users")

Wait For MagicAPI_Insertmaps(m As Map, success As Boolean)

If success Then
Log($"Record inserted: ${m}"$)
Else Log("Error inserting record.")
End If


Example 2: Insert Multiple Product Records

' Example usage - Insert Multiple Products:
Dim productRecords As List
productRecords.Initialize

Dim product1 As Map
product1.Initialize
product1.Put("name", "Keyboard")
product1.Put("price", 49.99)
product1.Put("stock", 50)

Dim product2 As Map
product2.Initialize
product2.Put("name", "Monitor")
product2.Put("price", 199.99)
product2.Put("stock", 20)

productRecords.AddAll(Array(product1, product2))

MagicAPI.InsertMultipleMaps(productRecords, "products")

Wait For MagicAPI_Insertmaps(m As Map, success As Boolean)

If success Then
Log($"Record inserted: ${m}"$)
Else
Log("Error inserting record.")
End If


Notes

  • Both routines provide asynchronous operations, and results are returned via their respective events.
  • Error handling is embedded, and failed operations trigger the appropriate event with a success = False.
  • Ensure the data maps are properly initialized and contain valid column names and values for the target table.


MagicFiles Module Documentation

The MagicFiles module is part of the Magic API library, designed to facilitate file and folder operations on a server via REST API. It supports functionalities such as uploading, downloading, listing, and deleting files or folders, and is compatible with file selection using ContentChooser in B4A.


Initialization

1. Initialize

Initializes the MagicFiles module with the necessary parameters to interact with the server.

Parameters:

  • Callback (Object): The object handling the event callbacks.
  • EventName (String): The name of the event that will be triggered upon completion.
  • urlbase (String): The base URL of the file server.
' Example usage:
Dim magicFiles As MagicFiles
magicFiles.Initialize(Me, "MagicFiles", "https://example.com/api")


File Operations

2. UploadFile

Uploads a file to the server.

Parameters:

  • filedir (String): Directory of the file.
  • filename (String): Name of the file to upload.
Event: <EventName>_UploadResult(Success As Boolean, FileName As String)

' Example usage:
magicFiles.UploadFile(File.DirRootExternal, "test.txt")

Sub MagicFiles_UploadResult(Success As Boolean, FileName As String)
If Success Then
Log($"File ${FileName} uploaded successfully"$)
Else
Log("Error uploading file")
End If
End Sub


3. UploadFileToFolder

Uploads a file to a specific folder on the server.

Parameters:

  • filedir (String): Directory of the file.
  • filename (String): Name of the file to upload.
  • targetFolder (String): Destination folder on the server.
  • newFileName (String): New name for the file on the server.
Event: <EventName>_UploadToFolderResult(Success As Boolean, FileName As String)

' Example usage:
magicFiles.UploadFileToFolder(File.DirRootExternal, "test.txt", "myFolder", "renamed.txt")

Sub MagicFiles_UploadToFolderResult(Success As Boolean, FileName As String)
If Success Then
Log($"File ${FileName} uploaded to folder successfully"$)
Else
Log("Error uploading file to folder")
End If
End Sub


4. UploadFileUri (B4A)

Uploads a file selected using ContentChooser to the server with a custom name.

Parameters:

  • uri (String): URI of the selected file.
  • newFileName (String): Name to save the file as on the server.
Event: <EventName>_UploadResultUri(Success As Boolean, FileName As String)

' Example usage with ContentChooser:
Private Sub Button1_Click
If chooser.IsInitialized = False Then
chooser.Initialize("chooser")
End If
chooser.Show("image/*", "Choose Image")
End Sub

Sub chooser_Result(Success As Boolean, Dir As String, FileName As String)
If Success Then
magicFiles.UploadFileUri(FileName, "uploaded_image.jpg")
Else Log("No file selected")
End If
End Sub

Sub MagicFiles_UploadResultUri(Success As Boolean, FileName As String)
If Success Then
Log($"File ${FileName} uploaded successfully"$)
Else
Log("Error uploading file")
End If
End Sub


5. UploadFileUri2 (B4A)

Uploads a file selected using ContentChooser by directly using its full path.

Parameters:

  • uri (String): URI of the selected file.
Event: <EventName>_UploadResultUri2(Success As Boolean, FileName As String)

' Example usage with ContentChooser:
Private Sub Button1_Click
If chooser.IsInitialized = False Then
chooser.Initialize("chooser")
End If
chooser.Show("[I]/[/I]", "Choose File")
End Sub

Sub chooser_Result(Success As Boolean, Dir As String, FileName As String)
If Success Then
magicFiles.UploadFileUri2(FileName)
Else
Log("No file selected")
End If
End Sub

Sub MagicFiles_UploadResultUri2(Success As Boolean, FileName As String)
If Success Then
Log($"File ${FileName} uploaded successfully with UploadFileUri2"$)
Else
Log("Error uploading file with UploadFileUri2")
End If
End Sub


6. ListFiles

Lists all files available on the server.

Event: <EventName>_ListSuccess(Success As Boolean, ListFiles As List)

' Example usage:
magicFiles.ListFiles

Sub MagicFiles_ListSuccess(Success As Boolean, ListFiles As List)
If Success Then
For Each fileName As String In ListFiles
Log($"File: ${fileName}"$)
Next
Else Log("Error listing files")
End If
End Sub


7. DeleteFile

Deletes a file from the server.

Parameters:

  • fileName (String): Name of the file to delete.
Event: <EventName>_DeleteFileSuccess(Success As Boolean, FileName As String)

' Example usage:
magicFiles.DeleteFile("test.txt")

Sub MagicFiles_DeleteFileSuccess(Success As Boolean, FileName As String)
If Success Then
Log($"File ${FileName} deleted successfully"$)
Else
Log("Error deleting file")
End If
End Sub


8. DeleteAllFiles

Deletes all files from the server.

Event: <EventName>_DeleteAllSuccess(Success As Boolean)

' Example usage:
magicFiles.DeleteAllFiles

Sub MagicFiles_DeleteAllSuccess(Success As Boolean)
If Success Then
Log("All files deleted successfully")
Else
Log("Error deleting all files")
End If
End Sub


Folder Operations

9. CreateFolder

Creates a new folder on the server.

Parameters:

  • FolderName (String): Name of the folder to create.
Event: <EventName>_CreateFolderSuccess(Success As Boolean, FolderName As String)

' Example usage:
magicFiles.CreateFolder("newFolder")

Sub MagicFiles_CreateFolderSuccess(Success As Boolean, FolderName As String)
If Success Then
Log($"Folder ${FolderName} created successfully"$)
Else
Log("Error creating folder")
End If
End Sub


10. MoveFileToFolder

Moves a file to another folder on the server.

Parameters:

  • FileName (String): Name of the file to move.
  • TargetFolder (String): Destination folder.
Event: <EventName>_MoveFileSuccess(Success As Boolean, TargetFolder As String)

' Example usage:
magicFiles.MoveFileToFolder("test.txt", "destinationFolder")

Sub MagicFiles_MoveFileSuccess(Success As Boolean, TargetFolder As String)
If Success Then
Log($"File moved to folder ${TargetFolder} successfully"$)
Else
Log("Error moving file")
End If
End Sub

11. DownloadFile​

Downloads a file from a server folder to a local directory.

Parameters:

  • TargetFolder (String): Folder on the server where the file is located.
  • FileName (String): Name of the file to download.
  • SaveDir (String): Local directory where the file will be saved.
  • SaveFileName (String): Name to save the file as locally.
Event: <EventName>_DownloadResult(Success As Boolean, FileName As String)

' Example usage:
magicFiles.DownloadFile("ServerFolder", "example.txt", File.DirRootExternal, "downloaded_example.txt")

Sub MagicFiles_DownloadResult(Success As Boolean, FileName As String)
If Success Then
Log($"File ${FileName} downloaded successfully."$)
Else
Log($"Error downloading the file: ${FileName}"$)
End If
End Sub


12. DownloadFile2​

Downloads a file from the server with range and progress tracking support.

Parameters:

  • Dir (String): Folder on the server where the file is located.
  • FileName (String): Name of the file to download.
  • SaveDir (String): Local directory where the file will be saved.
  • SaveFileName (String): Name to save the file as locally.
Events:

  • <EventName>_DownloadProgress(CurrentLength As Long, TotalLength As Long)
  • <EventName>_DownloadResult(Success As Boolean, FileName As String)
' Example usage with progress tracking:
magicFiles.DownloadFile2("ServerFolder", "largefile.zip", File.DirRootExternal, "largefile_local.zip")

Sub MagicFiles_DownloadProgress(CurrentLength As Long, TotalLength As Long)
Dim Progress As Float = (CurrentLength / TotalLength) * 100
Log($"Progress: ${Progress}%"$)
End Sub

Sub MagicFiles_DownloadResult(Success As Boolean, FileName As String)
If Success Then
Log($"File ${FileName} downloaded successfully."$)
Else
Log($"Error downloading the file: ${FileName}"$)
End If
End Sub




📂 Important Update for Magic API File Management 🚀

We’re introducing an enhanced file and folder management system with the latest version of Magic API! Here's how it works:


File and Folder Operations in Magic API

1️⃣ Default Folder: upload

  • All files and folders you create or interact with will be managed inside a new folder named upload.
  • This folder will be automatically created the first time you upload a file.
📍 Location: The upload folder will be created in the same directory as the files.php script on your server.


2️⃣ Automatic Folder Creation

  • When you add a file to a specific folder, Magic API will automatically create the folder if it doesn’t already exist.
  • This ensures seamless operations, eliminating the need for manual folder setup.

Examples

Uploading a File to the Default Folder

When uploading a file without specifying a folder, it will be stored directly in the upload folder.

' Example - Upload to Default Folder:
MagicFiles.UploadFile(File.DirRootExternal, "example.txt")

Wait For MagicFiles_UploadResult(Success As Boolean, FileName As String)

If Success Then
Log($"File ${FileName} uploaded successfully to the 'upload' folder."$)
Else
Log("Error uploading the file.")
End If


Uploading a File to a Specific Folder

If you specify a folder, Magic API will automatically create it if it doesn’t exist.

' Example - Upload to Specific Folder:
MagicFiles.UploadFileToFolder(File.DirRootExternal, "document.pdf", "tasks", "report.pdf")

Wait For MagicFiles_UploadToFolderResult(Success As Boolean, FileName As String)

If Success Then
Log($"File ${FileName} uploaded successfully to the 'tasks' folder."$)
Else
Log("Error uploading the file.")
End If


Why This Update?

✅ Automated Management: No need to manually create folders. Magic API does it for you!
✅ Simplified Operations: Focus on your app logic while the API handles the backend file structure.
✅ Consistency: All file operations are neatly organized within the upload folder.


Note

  • All file and folder operations (e.g., uploading, deleting, listing) will now occur inside the upload folder for better organization and security.
  • If you interact with a folder that doesn’t exist, it will be created automatically.


? Download Magic API Now

Thank you for your enthusiasm and support. Let the Magic API transform your B4X applications into powerful, connected experiences! ?✨
 
Last edited:

fernando1987

Active Member
Licensed User
🎉 Announcing Magic API Version 5: Revolutionize Your Database Management! 🚀

We are thrilled to unveil Magic API Version 5, now packed with even more powerful features to elevate your database management experience. This update introduces the brand-new MagicFiles module and several exciting additions, making this the most versatile release yet! 💡


🔥 What’s New in Version 5?

1. Introducing the MagicFiles Module 📂

The new MagicFiles module provides seamless file and folder management, allowing you to:

  • Upload Files: Effortlessly upload single or multiple files.
  • Download Files: Retrieve files from the server directly.
  • Manage Folders: Create, list, and delete folders with ease.
  • Advanced URI Support (B4A): Select and upload files directly using ContentChooser.
' Example - Upload a File Using ContentChooser:
Private Sub Button1_Click
If chooser.IsInitialized = False Then
chooser.Initialize("chooser")
End If
chooser.Show("[I]/[/I]", "Choose File")
End Sub

Sub chooser_Result(Success As Boolean, Dir As String, FileName As String)
If Success Then
MagicFiles.UploadFileUri(FileName, "new_file.jpg")
Else Log("No file selected")
End If End Sub

Sub MagicFiles_UploadResultUri(Success As Boolean, FileName As String)
If Success Then
Log($"File ${FileName} uploaded successfully"$)
Else Log("Error uploading file")
End If
End Sub


2. New InsertOrUpdateMapForColumnValue Method 🔄

Simplify your workflows with this powerful method that:

  • Checks if a record exists in the database by column value.
  • Updates the record if it exists or inserts a new record if it doesn’t.
' Example - Insert or Update a Record:
Dim userData As Map
userData.Initialize
userData.Put("name", "John Doe")
userData.Put("email", "[email protected]")
userData.Put("age", 30)

MagicAPI.InsertOrUpdateMapForColumnValue("users", "email", "[email protected]", userData)

Wait For MagicAPI_InsertOrUpdateMapForColumnValue(message As String, success As Boolean)

If success Then
Select Case message
Case "Insert"
Log("A new record was inserted.")
Case "Update"
Log("The record was updated.")
End Select
Else Log("Error performing the operation.")
End If


3. New InsertMultipleMaps Method 💾

Boost your efficiency by inserting multiple records at once with a single method call.

' Example - Insert Multiple Records:
Dim records As List records.Initialize

Dim user1 As Map
user1.Initialize
user1.Put("name", "Alice")
user1.Put("email", "[EMAIL][email protected][/EMAIL]")
user1.Put("age", 25)

Dim user2 As Map
user2.Initialize
user2.Put("name", "Bob")
user2.Put("email", "[EMAIL][email protected][/EMAIL]")
user2.Put("age", 28)

records.AddAll(Array(user1, user2))

MagicAPI.InsertMultipleMaps(records, "users")

Wait For MagicAPI_Insertmaps(m As Map, success As Boolean)

If success Then
Log($"Record inserted: ${m}"$)
Else Log("Error inserting records.")
End If


Why Upgrade to Version 5?

✅ Enhanced Productivity: New tools to manage files and records effortlessly.
✅ Seamless Integration: Works flawlessly with your existing Magic API setup.
✅ Intuitive Design: Designed to save you time and make your workflows smoother.


📋 Examples and Documentation
To help you get started, detailed examples and full documentation for the new routines and the MagicFiles module have been added to Post 1 of this thread. Check them out to see how easy it is to integrate these features into your projects!


📥 How to Update if You’re an Existing Customer
If you’ve already purchased Magic API, here’s how you can get Version 5:
1️⃣ Log in to your account.
2️⃣ Navigate to the downloads section.
3️⃣ Download the latest update.


✨ Experience the Power of Magic API Version 5 Today! ✨
Got questions? Need help? Feel free to comment below or contact our support team. 🚀
 
Last edited:

fernando1987

Active Member
Licensed User

📂 Important Update for Magic API File Management 🚀

We’re introducing an enhanced file and folder management system with the latest version of Magic API! Here's how it works:


File and Folder Operations in Magic API

1️⃣ Default Folder: upload

  • All files and folders you create or interact with will be managed inside a new folder named upload.
  • This folder will be automatically created the first time you upload a file.
📍 Location: The upload folder will be created in the same directory as the files.php script on your server.


2️⃣ Automatic Folder Creation

  • When you add a file to a specific folder, Magic API will automatically create the folder if it doesn’t already exist.
  • This ensures seamless operations, eliminating the need for manual folder setup.

Examples

Uploading a File to the Default Folder

When uploading a file without specifying a folder, it will be stored directly in the upload folder.

' Example - Upload to Default Folder:
MagicFiles.UploadFile(File.DirRootExternal, "example.txt")

Wait For MagicFiles_UploadResult(Success As Boolean, FileName As String)

If Success Then
Log($"File ${FileName} uploaded successfully to the 'upload' folder."$)
Else
Log("Error uploading the file.")
End If


Uploading a File to a Specific Folder

If you specify a folder, Magic API will automatically create it if it doesn’t exist.

' Example - Upload to Specific Folder:
MagicFiles.UploadFileToFolder(File.DirRootExternal, "document.pdf", "tasks", "report.pdf")

Wait For MagicFiles_UploadToFolderResult(Success As Boolean, FileName As String)

If Success Then
Log($"File ${FileName} uploaded successfully to the 'tasks' folder."$)
Else
Log("Error uploading the file.")
End If


Why This Update?

✅ Automated Management: No need to manually create folders. Magic API does it for you!
✅ Simplified Operations: Focus on your app logic while the API handles the backend file structure.
✅ Consistency: All file operations are neatly organized within the upload folder.


Note

  • All file and folder operations (e.g., uploading, deleting, listing) will now occur inside the upload folder for better organization and security.
  • If you interact with a folder that doesn’t exist, it will be created automatically.
 

fernando1987

Active Member
Licensed User

✨ Magic API Library 5.1 – Powerful New File Management Features! 🚀

We’re thrilled to announce the release of Magic API Library 5.1! This update introduces two highly-requested features for downloading files from your server directly to your local directory – now with optional range support and real-time progress notifications! 📁⚡

📌 What’s New in Version 5.1?​

1️⃣ DownloadFile – Download files directly from a server folder to your local directory.

  • Triggers the DownloadResult event to notify the download status.
2️⃣ DownloadFile2 – Enables file downloads with range support and progress tracking.

  • Fires two events:
    • DownloadProgress: Provides real-time updates.
    • DownloadResult: Notifies the final result of the download.
These new methods are designed to simplify file management while maintaining top performance for Magic API users.


🛠 Example 1: Simple File Download with DownloadFile​

' Example - Download File:
MagicFiles.DownloadFile("ServerFolder", "example.txt", File.DirRootExternal, "downloaded_example.txt")

Wait For MagicFiles_DownloadResult(Success As Boolean, FileName As String)

If Success Then
Log($"File ${FileName} downloaded successfully."$)
Else
Log($"Error downloading the file: ${FileName}"$)
End If


🛠 Example 2: File Download with Progress and Range Support using DownloadFile2​

' Example - Download File with Progress:
MagicFiles.DownloadFile2("ServerFolder", "largefile.zip", File.DirRootExternal, "largefile_local.zip")

Wait For MagicFiles_DownloadProgress(CurrentLength As Long, TotalLength As Long)

Dim Progress As Float = (CurrentLength / TotalLength) * 100
Log($"Progress: ${Progress}%"$)

Wait For MagicFiles_DownloadResult(Success As Boolean, FileName As String)

If Success Then
Log($"File ${FileName} downloaded successfully."$)
Else
Log($"Error downloading the file: ${FileName}"$)
End If


📋 Examples and Documentation
To help you get started, detailed examples and full documentation for the new routines and the MagicFiles module have been added to Post 1 of this thread. Check them out to see how easy it is to integrate these features into your projects!

📥 How to Update if You’re an Existing Customer
If you’ve already purchased Magic API, here’s how to get Version 5.1:
1️⃣ Log in to your account.
2️⃣ Navigate to the downloads section.
3️⃣ Download the latest update.


 

vecino

Well-Known Member
Licensed User
Longtime User
Hello, only for mysql, can it be used for postgresql?
Thank you.
 

Claudio Oliveira

Active Member
Licensed User
Longtime User

fernando1987

Active Member
Licensed User

🌟 Magic API Library 6.0 is Here! New Features for Your B4X Apps 🚀


Hello, B4X community! We’re thrilled to unveil Magic API Library 6.0, the tool that transforms your MySQL databases into a powerful, secure RESTful API in minutes. This update introduces new functions in the api.php file and the class for flexible data management, with updated examples and documentation in the original thread’s first post. Discover what’s new! 💻✨


🎉 New Features in Version 6.0​


  • ClearTable: New function to clear all records from a table without deleting its structure, ideal for resetting data.
  • SumColumn: Sums the values in a specific column, perfect for aggregation calculations like totals.
  • CountRows: Counts the total number of rows in a table, useful for quick statistics.
  • CountValue: Counts occurrences of a specific value in a column, enabling detailed analysis.
  • CreateTable: Creates new tables in the database directly from the API, simplifying schema management.
  • CreateColumn: Adds new columns to existing tables, allowing dynamic schema modifications.
  • DeleteColumn: Removes columns from tables, providing greater control over database schema.
  • CustomQuery: Executes custom SQL queries (limited to SELECT for security), offering maximum flexibility.
  • DeleteTable: Deletes entire tables from the database, ideal for cleanups or restructuring.
  • JoinTables: Performs joins across multiple tables for complex queries, optimizing data combination.
  • Percentage Calculation: New functionality to calculate percentages of values in a column, ideal for statistical analysis.

Own a previous version? Log into the official store, go to "Recent Orders," and download version 6.0 at no extra cost.


📚 Updated Documentation and Examples​


We’ve refreshed the original thread’s first post with:


  • Code examples for the new functions (ClearTable, SumColumn, CountRows, CountValue, CreateTable, CreateColumn, DeleteColumn, CustomQuery, DeleteTable, JoinTables, and percentage calculation).
  • Detailed instructions for setting up and using these features in your projects.
  • Practical examples for B4XPages, B4A, B4I, and B4J, including cURL equivalents for direct testing.

🛒 Get Magic API 6.0 Now!​


Elevate your B4X apps with these powerful new capabilities. Download the library and api.php from the official store
 
Top