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.4^ # Replace with the latest version

Then run:

flutter pub get

βš™οΈ Setup Guide

1. Download the PHP Script

Link - https://github.com/amazingsammed/sqlbase/blob/master/asset/sqlbase.php

πŸ‘‰ Download the PHP Script

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 = 'YourAddress';
$dbname = 'YourDatabaseName';
$username = 'YourUsername';
$password = 'YourPassword';

// API key must match the one used in your Flutter app
$apiKey = '123456';

4. Initialize the Plugin in Flutter

Sqlbase.initialize(url: "http://localhost/sqlbase.php", key: '123456');

πŸ“˜ Usage Guide

Initialize

Sqlbase.initialize(url: "http://localhost/sqlbase.php", key: '123456');
final myDB = Sqlbase();

πŸ” Read Data

await myDB.table("users").get();

//OR

await SqlBase.select([]).from(tablename).execute();

βž• Insert Data

await myDB.table("users").add({
  "name": "Flutter",
  "year": 2015,
});

// OR

await SqlBase.insertInto(tablename).values({
    "name":"Sammed"
}).execute();

✏️ Update Data

await myDB.table("users").record("1", column: "id").update({
  "name": "Next.js",
  "year": 2019,
});

// or

await Sqlbase.update('user').where('id', '1').set({
 "name": "Sammed"
  }).execute();

❌ Delete Data

await myDB.table("users").record("1", column: "id").delete();

// or

await Sqlbase.deleteFrom('user').where('id', '1').execute();

🧰 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
  • Validate all user inputs to prevent SQL injection
  • 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();

πŸ§ͺ Testing

To test locally:

  1. Use a tool like Postman to verify the PHP script response
  2. Check database connection and permissions
  3. Enable PHP error display for debugging:
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    

🀝 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.