LibSQL Class
executeBatch()
LibSQL PHP Extension method executeBatch()
The executeBatch()
method in the LibSQL PHP Extension facilitates the execution of a batch of SQL statements on the database. This method is particularly useful when multiple SQL statements need to be executed together as a single unit, offering efficiency and atomicity in database operations.
Method Signature
public function executeBatch(string $stmt): bool
Parameters
$stmt
(string): The batch of SQL statements to execute as a single string.
Return Value
bool
: Returnstrue
if the batch execution was successful, otherwisefalse
.
Example
// Create a new LibSQL instance
$db = new LibSQL("database.db");
// SQL statements to execute as a batch
$stmt = "
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER
);
INSERT INTO users (name, age) VALUES ('John Doe', 30);
INSERT INTO users (name, age) VALUES ('Jane Smith', 25);
INSERT INTO users (name, age) VALUES ('Michael Johnson', 40);
";
// Execute the batch of SQL statements
if ($db->executeBatch($stmt)) {
echo "Batch execution success.";
} else {
echo "Batch execution failed.";
}
Notes
- SQL statements within the batch are separated by semicolons (
;
). - This method executes all SQL statements within the batch as a single transaction, ensuring atomicity.
- It is important to ensure that the SQL statements within the batch are logically related and do not violate database integrity constraints.
- Batch execution can improve performance by reducing the overhead of multiple database connections and transactions. However, it should be used judiciously to avoid excessive load on the database server.