exec(query) : Executes a query based on SQLite3 connection. <?php
$my_conn = new SQLite3('my_db.db'); // Connect to existing database
$sql="DELETE FROM student WHERE class='Four'";
$my_conn->exec($sql); // Executes the query
?>
We can check by reading the return value ( True or False ) and use if condition to give message. <?php
$my_conn = new SQLite3('my_db.db'); // Connect to existing database
$sql="DELETE FROM student WHERE class='Four'";
$result = $my_conn->exec($sql); // Executes the query
if ($result) {
echo 'Number of rows deleted : ', $my_conn->changes();
}
?>
Output
Number of rows deleted : 9
Here we are not expecting any return of rows or data from the database so we should use exec(). However if we expect any data returned from query operation then it is better to use query(). <?php
$my_conn = new SQLite3('my_db.db');
$sql="UPDATE student SET mark=mark+5 WHERE class='Five'";
$result=$my_conn->exec($sql); // connection with Query
if ($result) {
echo 'Number of rows Updated : ', $my_conn->changes();
}
?>
Output
Number of rows Updated : 3
<?php
$my_conn = new SQLite3('my_db.db');
$sql="INSERT INTO `student`
(`id`, `name`, `class`, `mark`, `gender`) VALUES
(39, 'Abcd', 'Four', 88, 'Female'),
(40, 'XYZ', 'Five', 38, 'Male')";
$result = $my_conn->exec($sql);
if ($result) {
echo 'Number of rows added : ', $my_conn->changes();
}
?>
Output
Number of rows added : 2
$sql="CREATE TABLE IF NOT EXISTS
student(id integer primary key,
name text,
class text,
mark integer,
gender text
)";
$my_conn->exec($sql);
<?php
$my_conn = new SQLite3('my_db.db');
$sql="UPDATE student SET mark=mark+5 WHERE class='Five';
DELETE FROM student WHERE class='Three';
DELETE FROM student WHERE class='Six';
DELETE FROM student";
$result=$my_conn->exec($sql); // connection with Query
if ($result) {
echo 'Number of rows changed : ', $my_conn->changes();
}
?>
<?Php
$my_conn = new SQLite3('my_db.db'); // create database in same location
//$sql="SELECT * FROM student WHERE class3='Five'";
$sql="INSERT INTO `student`
(`id`, `name`, `class`, `mark`, `gender`) VALUES
(1, 'John Deo', 'Four', 75, 'female')";
$result=$my_conn->exec($sql); // Create table
if($result==FALSE){
echo "Error in Message : ".$my_conn->lastErrorMsg();
echo "<BR>Error Code : ".$my_conn->lastErrorCode();
}else{
echo 'Number of rows inserted: ', $my_conn->changes();
}
?>
Output
Error in Message : UNIQUE constraint failed: student.id
Error Code : 19
If you change the query like this ( commented line )
$sql="SELECT * FROM student WHERE class3='Five'";
The error code and message will be like this .
Error in Message : no such column: class3
Error Code : 1
$my_conn = new SQLite3('my_db.db'); // create database in same location
$my_conn->enableExceptions(true);
try {
$my_conn->exec('create table abcd'); // Wrong query
} catch (Exception $e) {
echo 'Caught exception: ' . $e->getMessage();
echo "<BR>Error in Message : ".$my_conn->lastErrorMsg();
}
Output
Caught exception: incomplete input
Error in Message : incomplete input
<?php
$my_conn = new SQLite3('my_db.db'); // create database in same location
$sql="INSERT INTO `student`
( `name`, `class`, `mark`, `gender`) VALUES
('John Deo', 'Four', 75, 'female')";
$result=$my_conn->exec($sql); // Create table
echo 'Number of rows inserted: ', $my_conn->changes();
echo '<BR>Last inserted row ID : ', $my_conn->lastInsertRowID() ;
?>
Output is here
Number of rows inserted: 1
Last inserted row ID : 37
We can use exec() with VACUUM ( To clean the database )
$my_conn = new SQLite3('my_db.db'); // create database in same location
$result=$my_conn->exec('VACUUM;'); //
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.