SQLbase
SQLbase is a lightweight Flutter plugin that enables seamless communication between a Flutter app and an SQL database through a simple PHP backend. Itβs designed to function similarly to Firebase queries, making it easy and intuitive to use.
π Features
- Connects Flutter with MySQL using PHP
- Easy query interface inspired by Firebase
- Lightweight and easy to integrate
- Supports CRUD operations (Create, Read, Update, Delete)
- Supports advanced SQL features like filtering, ordering, and grouping
π¦ Installation
Add sqlbase to your pubspec.yaml file:
dependencies:
sqlbase: 0.0.8^ # Replace with the latest version
Then run:
flutter pub get
βοΈ Setup Guide
1. Download the PHP Script
Old script
Link - https://github.com/amazingsammed/sqlbase/blob/master/asset/sqlbase.php
New script
Link - https://github.com/amazingsammed/sqlbase/blob/master/asset/sqlbase2.php
2. Deploy the Script
Place the downloaded PHP file in your serverβs root directory:
- For WAMP, place it in the
www/folder - For other servers, use the respective public folder
3. Configure the PHP Script
Open the script and set your database configuration:
$host = 'Your_Address';
$dbname = 'Your_Database_Name';
$username = 'Your_Username';
$password = 'Your_Password';
// API key must match the one used in your Flutter app
$apiKey = '123456';
π Usage Guide
Initialize
final Sqlbase.initialize(url: "http://localhost/sqlbase.php", key: '123456');
final myDB = Sqlbase();
/// Read Data
await myDB.table("users").get();
/// Insert Data
await myDB.table("users").add({
"name": "Flutter",
"year": 2015,
});
/// Update Data
await myDB.table("users").record("1", column: "id").update({
"name": "Next.js",
"year": 2019,
});
/// Delete Data
await myDB.table("users").record("1", column: "id").delete();
Potential errors that can occur
- Contact The Developer: Error Key
- Solution is to change the Authorization key in the php to start with capital A
/// change from
if (!isset($header['authorization']) || str_replace("Bearer ", "", $header['authorization']) !== $accessToken) {
die(json_encode(['error' => 'Contact The Developer: Error Key']));
}
if (!isset($header['Authorization']) || str_replace("Bearer ", "", $header['Authorization']) !== $accessToken) {
die(json_encode(['error' => 'Contact The Developer: Error Key']));
}
Database Methods
.table("column").select([]).insertInto("tablename").deleteFrom("tablename").auth("tablename")
Table Methods
.where("column", isEqualTo: "value").isEqualTo({"column": "value"}).limit(10).orderBy("column", "ASC" or "DESC").groupBy("column").get().add(Map<String, dynamic> data).addMany(List<Map<String, dynamic>> dataList)
Record Methods
.update(Map<String, dynamic> newData).delete()
Best Practices
- Ensure the API key in your PHP file and Flutter app match
- Use HTTPS in production for secure communication
- Use environment variables or a secure config file for DB credentials
Examples
// Read with condition
await myDB.table("products").where("price", isEqualTo: 100).get();
// Group and order
await myDB.table("users").groupBy("gender").orderBy("id", "DESC").limit(5).get();
π€ Contributing
We welcome contributions! Feel free to fork the repo and submit pull requests.
π GitHub: https://github.com/amazingsammed/sqlbase
π License
MIT License. See LICENSE for details.