Download SQLite 3 php interface,mp4
SQLite3 is a lightweight, file-based database engine that can be easily integrated with PHP without the need for a separate server process (like MySQL or PostgreSQL). PHP comes with built-in support for SQLite3, making it great for small-to-medium applications, local storage, or prototyping.
Here’s a full guide on using the SQLite3 PHP interface 👇
🧱 1. Enabling SQLite3 in PHP
Most modern PHP installations already have SQLite3 enabled by default.
To confirm:
php -m | grep sqlite
You should see sqlite3 in the list.
If not, enable it in your php.ini by uncommenting:
extension=sqlite3
Restart your web server afterward.
⚙️ 2. Creating and Connecting to an SQLite3 Database
SQLite databases are just files on disk. You can open (or create) one using the SQLite3 class.
<?php
// Create or open a database file
$db = new SQLite3('example.db');
// Check connection
if ($db) {
echo "Connected to SQLite3 database successfully!";
}
?>
This will create a file named example.db in your current directory.
📄 3. Creating a Table
<?php
$db = new SQLite3('example.db');
// Create a table
$query = "CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL
)";
$db->exec($query);
echo "Table created successfully!";
?>
✏️ 4. Inserting Data
<?php
$db = new SQLite3('example.db');
$stmt = $db->prepare("INSERT INTO users (name, email) VALUES (:name, :email)");
$stmt->bindValue(':name', 'Alice');
$stmt->bindValue(':email', 'alice@example.com');
$stmt->execute();
echo "Data inserted!";
?>
🔍 5. Querying Data
<?php
$db = new SQLite3('example.db');
$results = $db->query("SELECT * FROM users");
while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
echo "ID: {$row['id']} | Name: {$row['name']} | Email: {$row['email']} <br>";
}
?>
🧹 6. Updating and Deleting Data
Update Example:
$db->exec("UPDATE users SET email='alice_new@example.com' WHERE id=1");
Delete Example:
$db->exec("DELETE FROM users WHERE id=1");
🛠️ 7. Using PDO with SQLite (Alternative Method)
PDO provides a more flexible and modern way to interact with databases.
<?php
try {
$pdo = new PDO('sqlite:example.db');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)");
$pdo->exec("INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com')");
foreach ($pdo->query("SELECT * FROM users") as $row) {
echo $row['name'] . " - " . $row['email'] . "<br>";
}
} catch (PDOException $e) {
echo $e->getMessage();
}
?>
🧠 8. When to Use SQLite3 in PHP
✅ Great for:
-
Small websites or internal tools
-
Local data storage
-
Desktop/web hybrid apps
-
Prototyping before using MySQL/PostgreSQL
🚫 Not ideal for:
-
High-concurrency web apps
-
Large-scale data sets
-
Distributed systems
Enjoy! Follow us for more...


No comments:
Post a Comment