SQLite Tutorial

  • Home
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • Interfaces
    • SQLite Java
    • SQLite Node.js
    • SQLite PHP
    • SQLite Python
  • Try It
Home / SQLite PHP / SQLite PHP: Deleting Data

SQLite PHP: Deleting Data

Summary: in this tutorial, we will show you steps of deleting data in an SQLite database using PHP PDO.

Steps for deleting data from PHP PDO

To delete data in a table in the SQLite database from a PHP application, you use these steps:

  1. Connect to the SQLite database by creating an instance of the PDO class.
  2. Prepare a DELETE statement for execution.
  3. Pass values to the statement using the bindValue() method of the PDOStatement object.
  4. Call the execute() method of the PDOStatement object to execute the delete statement.
  5. Call the rowCount() method of the PDOStatement to get the number of rows deleted.

SQLite PHP: delete data examples

We will use the projects and tasks that we created in the previous tutorial for the demonstration.

First, we create the SQLiteDelete class in the app folder.

The following deleteTask() method deletes a task by task_id. The method returns the number of rows deleted by calling the rowCount() method of the PDOStatement object.

We use the bindValue() method to pass task id value to the DELETE statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    /**
     * Delete a task by task id
     * @param int $taskId
     * @return int the number of rows deleted
     */
    public function deleteTask($taskId) {
        $sql = 'DELETE FROM tasks '
                . 'WHERE task_id = :task_id';
 
        $stmt = $this->pdo->prepare($sql);
        $stmt->bindValue(':task_id', $taskId);
 
        $stmt->execute();
 
        return $stmt->rowCount();
    }

The deleteTaskByProject() method deletes all tasks associated with a project specified by the project id.

In this method, instead of using the bindValue() method to pass values to the DELETE statement, we pass the input values as an array to the execute() method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    /**
     * Delete all tasks associated with a project
     * @param int $projectId
     * @return int the number of rows deleted
     */
    public function deleteTaskByProject($projectId) {
        $sql = 'DELETE FROM tasks '
                . 'WHERE project_id = :project_id';
 
        $stmt = $this->pdo->prepare($sql);
 
        $stmt->execute([':project_id' => $projectId]);
 
        return $stmt->rowCount();
    }

The following method deletes a project specified by a project id. It also returns the number of the rows in the projects table deleted.

In addition, due to the foreign key constraint, all the tasks associated with the project are also deleted. The rowCount() function does not consider the deleted rows in the tasks table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    /**
     * Delete the project by project id
     * @param int $projectId
     * @return int the number of rows deleted
     */
    public function deleteProject($projectId) {
        $sql = 'DELETE FROM projects '
                . 'WHERE project_id = :project_id';
 
        $stmt = $this->pdo->prepare($sql);
 
        $stmt->execute([':project_id' => $projectId]);
 
        return $stmt->rowCount();
    }

Second, we create the index.php script to test the SQLiteDelete class.

1
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
<?php
 
require 'vendor/autoload.php';
 
use App\SQLiteConnection as SQLiteConnection;
use App\SQLiteDelete as SQLiteDelete;
 
$pdo = (new SQLiteConnection())->connect();
$sqlite = new SQLiteDelete($pdo);
 
// delete task id 1
$taskId = 1;
$rowDeleted = $sqlite->deleteTask($taskId);
 
echo 'The number of rows deleted: ' . $rowDeleted . '<br>';
 
// delete task associated with a project id 1
$projectId = 1;
$sqlite->deleteTaskByProject($projectId);
 
echo 'The number of task in the project #' . $projectId . ' deleted: ' . $rowDeleted . '<br>';
 
// delete project with id 1 and also its associated tasks
$projectId = 2;
$sqlite->deleteProject(2);
echo 'The number of project deleted: ' . $rowDeleted . '<br>';

The following is the output of the script:

1
2
3
The number of rows deleted: 1
The number of task in the project #1 deleted: 1
The number of project deleted: 1

In this tutorial, we have shown you how to delete data in one or more tables in the SQLite database using PHP PDO.

  • Was this tutorial helpful ?
  • Yes   No
Previous Tutorial: SQLite PHP: Transaction

Getting Started

  • What Is SQLite
  • Download & Install SQLite
  • SQLite Sample Database
  • SQLite Commands

SQLite Tutorial

  • SQLite Select
  • SQLite Order By
  • SQLite Select Distinct
  • SQLite Where
  • SQLite Limit
  • SQLite BETWEEN
  • SQLite IN
  • SQLite Like
  • SQLite GLOB
  • SQLite Left Join
  • SQLite Inner Join
  • SQLite Cross Join
  • SQLite Self-Join
  • SQLite Full Outer Join
  • SQLite Group By
  • SQLite Having
  • SQLite Union
  • SQLite Except
  • SQLite Intersect
  • SQLite Subquery
  • SQLite EXISTS
  • SQLite Case
  • SQLite Insert
  • SQLite Update
  • SQLite Delete
  • SQLite Replace
  • SQLite Transaction

SQLite Data Definition

  • SQLite Data Types
  • SQLite Date & Time
  • SQLite Create Table
  • SQLite Primary Key
  • SQLite Foreign Key
  • SQLite NOT NULL Constraint
  • SQLite UNIQUE Constraint
  • SQLite CHECK Constraint
  • SQLite AUTOINCREMENT
  • SQLite Alter Table
  • SQLite Drop Table
  • SQLite Create View
  • SQLite Index
  • SQLite Expression Based Index
  • SQLite Trigger
  • SQLite VACUUM
  • SQLite Transaction
  • SQLite Full-text Search

SQLite Tools

  • SQLite Commands
  • SQLite Show Tables
  • SQLite Describe Table
  • SQLite Dump
  • SQLite Import CSV
  • SQLite Export CSV

SQLite Functions

  • SQLite AVG
  • SQLite COUNT
  • SQLite MAX
  • SQLite MIN
  • SQLite SUM

SQLite Interfaces

  • SQLite PHP
  • SQLite Node.js
  • SQLite Java
  • SQLite Python

About SQLite Tutorial

SQLite Tutorial website helps you master SQLite quickly and easily. It explains the complex concepts in simple and easy-to-understand ways so that you can both understand SQLite fast and know how to apply it in your software development work more effectively.

Looking for a tutorial…

If you did not find the tutorial that you are looking for, you can use the following search box. In case the tutorial is not available, you can request for it using the request for a SQLite tutorial form.

Recent Tutorials

  • SQLite Window Frame
  • SQLite CUME_DIST
  • SQLite PERCENT_RANK
  • SQLite DENSE_RANK
  • SQLite NTILE
  • SQLite NTH_VALUE
  • SQLite LAST_VALUE
  • SQLite FIRST_VALUE

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2019 SQLite Tutorial. All rights Reserved.

⤒