How to Use ServBay's Built-in SQLite Module
As a powerful integrated web development tool, ServBay comes with an SQLite module that is easy to enable. SQLite is a lightweight, embedded relational database management system widely used in modern web development. With ServBay, developers can easily enable the SQLite module to use SQLite for data storage and management in PHP applications.
Introduction to the SQLite Module
SQLite is an open-source embedded relational database that does not require a separate server process and can be directly embedded into applications. Known for its simplicity, ease of use, and efficiency, SQLite is ideal for small to medium-sized applications and development environments.
Main Features
- Lightweight: SQLite is a very lightweight database system suitable for embedding into various applications.
- Zero Configuration: SQLite does not require a separate server process or complex configuration; it is ready to use out of the box.
- High Performance: For small to medium-scale data processing, SQLite provides efficient read and write performance.
- Single File Storage: SQLite stores the entire database in a single file, making it easy to manage and back up.
- ACID Transaction Support: SQLite supports Atomicity, Consistency, Isolation, and Durability (ACID) transactions, ensuring the reliability of the data.
Version of SQLite Module Built into ServBay
ServBay supports multiple PHP versions and pre-installs and enables the corresponding SQLite module by default for each version.
How to Enable the SQLite Module
By default, the SQLite module is enabled and requires no additional configuration.
Using SQLite in PHP Code
After enabling the SQLite module, you can use SQLite for database operations in PHP code. Here are two simple examples, one using the standard SQLite3 method and the other using the PDO method.
Example Code (Standard SQLite3 Method)
<?php
// Connect to the SQLite database
$db = new SQLite3('servbay_db.sqlite');
// Create a table
$db->exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, email TEXT, age INTEGER)");
// Insert data
$stmt = $db->prepare("INSERT INTO users (name, email, age) VALUES (:name, :email, :age)");
$stmt->bindValue(':name', 'ServBay', SQLITE3_TEXT);
$stmt->bindValue(':email', '[email protected]', SQLITE3_TEXT);
$stmt->bindValue(':age', 30, SQLITE3_INTEGER);
$stmt->execute();
// Query data
$result = $db->query("SELECT * FROM users WHERE name = 'ServBay'");
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
echo "Queried data: ";
print_r($row);
}
// Close the database connection
$db->close();
?>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Example Code (PDO Method)
<?php
try {
// Create a new PDO instance
$dsn = 'sqlite:servbay_db.sqlite';
$pdo = new PDO($dsn);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Create a table
$pdo->exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, email TEXT, age INTEGER)");
// Insert data
$stmt = $pdo->prepare("INSERT INTO users (name, email, age) VALUES (:name, :email, :age)");
$stmt->execute([
':name' => 'ServBay',
':email' => '[email protected]',
':age' => 30
]);
echo "Data inserted successfully";
// Query data
$stmt = $pdo->prepare("SELECT * FROM users WHERE name = :name");
$stmt->execute([':name' => 'ServBay']);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "Queried data: ";
print_r($data);
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
// Close the database connection (PDO will automatically close the connection)
?>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Conclusion
ServBay provides a convenient way to manage and enable the SQLite module. With simple configuration and restart operations, developers can quickly enable the SQLite module in different PHP versions, allowing them to use SQLite for data storage and management in PHP applications. SQLite's lightweight, zero-configuration, and high performance make it an indispensable database solution in modern web development. With ServBay and SQLite, developers can build efficient and simple web applications.