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: Inserting Data

SQLite PHP: Inserting Data

Summary: in this tutorial, we will show you how to use PHP PDO to insert data into SQLite tables.

We will use the projects and tasks table that we created in the creating table tutorial.

To insert data into a table, you follow these steps:

  1. Connect to the SQLite database by creating a new PDO instance with the path to the SQLite database file.
  2. Compose an INSERT statement. If you want to supply the values for the INSERT statement, you use the named placeholders in the form :name which name is the parameter that you will provide the value.
  3. Call the prepare() method of the PDO object to prepare the INSERT statement for execution. This method returns a PDOStatement object.
  4. Bind values to the parameters by calling the bindValue() method of the PDOStatement object.
  5. Execute the statement by calling the execute() method of the PDOStatement object.
  6. In case the table has a generated autoincrement column as the primary key, you can get the inserted id by calling the lastInsertId() method of the PDO object.

The following SQLiteInsert class has two methods for inserting data into tables. The insertProject() method inserts a new project into the projects table and the insertTask() method inserts a new task into the tasks table.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
 
namespace App;
 
/**
* PHP SQLite Insert Demo
*/
class SQLiteInsert {
 
    /**
     * PDO object
     * @var \PDO
     */
    private $pdo;
 
    /**
     * Initialize the object with a specified PDO object
     * @param \PDO $pdo
     */
    public function __construct($pdo) {
        $this->pdo = $pdo;
    }
 
    /**
     * Insert a new project into the projects table
     * @param string $projectName
     * @return the id of the new project
     */
    public function insertProject($projectName) {
        $sql = 'INSERT INTO projects(project_name) VALUES(:project_name)';
        $stmt = $this->pdo->prepare($sql);
        $stmt->bindValue(':project_name', $projectName);
        $stmt->execute();
 
        return $this->pdo->lastInsertId();
    }
 
    /**
     * Insert a new task into the tasks table
     * @param type $taskName
     * @param type $startDate
     * @param type $completedDate
     * @param type $completed
     * @param type $projectId
     * @return int id of the inserted task
     */
    public function insertTask($taskName, $startDate, $completedDate, $completed, $projectId) {
        $sql = 'INSERT INTO tasks(task_name,start_date,completed_date,completed,project_id) '
                . 'VALUES(:task_name,:start_date,:completed_date,:completed,:project_id)';
 
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute([
            ':task_name' => $taskName,
            ':start_date' => $startDate,
            ':completed_date' => $completedDate,
            ':completed' => $completed,
            ':project_id' => $projectId,
        ]);
 
        return $this->pdo->lastInsertId();
    }
 
}

In the insertTask() method, instead of binding values, we pass the inserted values as an array to the execute() method.

We use the following code in the index.php file to insert projects and their associated tasks into the projects and tasks table.

SQLite PHP Insert Demo

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
 
require 'vendor/autoload.php';
 
use App\SQLiteConnection;
use App\SQLiteInsert;
 
$pdo = (new SQLiteConnection())->connect();
$sqlite = new SQLiteInsert($pdo);
 
// insert a new project
$projectId = $sqlite->insertProject('PHP SQLite Demo');
// insert some tasks for the project
$sqlite->insertTask('Prepare the sample database schema', '2016-06-01', '2016-06-01', 1, $projectId);
$sqlite->insertTask('Create new tables ', '2016-05-01', null, 0, $projectId);
$sqlite->insertTask('Insert some sample data', '2016-05-01', '2016-06-02', 1, $projectId);
 
// insert a second project
$projectId = $sqlite->insertProject('Mastering SQLite');
// insert the tasks for the second project
$sqlite->insertTask('Go to sqlitetutorial.net', '2016-06-01', null, 0, $projectId);
$sqlite->insertTask('Read all the tutorials.', '2016-06-01', null, 0, $projectId);
$sqlite->insertTask('Use Try It page to practice the SQLite commands.', '2016-06-01', null, 0, $projectId);
$sqlite->insertTask('Develop a simple SQLite-based application', '2016-06-15', null, 0, $projectId);

In this tutorial, we have shown you how to use the PDOStatement to insert data into the SQLite tables.

  • Was this tutorial helpful ?
  • YesNo
Previous Tutorial: SQLite PHP: Creating Tables
Next Tutorial: SQLite PHP: Update Data

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 IS NULL
  • SQLite GLOB
  • SQLite Join
  • SQLite Inner Join
  • SQLite Left 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 Rename Column
  • SQLite Drop Table
  • SQLite Create View
  • SQLite Drop 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 Join
  • SQLite IS NULL
  • SQLite Rename Column
  • SQLite DROP VIEW
  • SQLite Window Frame
  • SQLite CUME_DIST
  • SQLite PERCENT_RANK
  • SQLite DENSE_RANK

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2019 SQLite Tutorial. All rights Reserved.

⤒