Sie sind auf Seite 1von 3

http://www.sqlitetutorial.

net/sqlite-php/insert/

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 <?php
2
3 namespace App;
4
5 /**
6 * PHP SQLite Insert Demo
7 */
8 class SQLiteInsert {
9
10 /**
11 * PDO object
12 * @var \PDO
13 */
14 private $pdo;
15
16 /**
17 * Initialize the object with a specified PDO object
18 * @param \PDO $pdo
19 */
20 public function __construct($pdo) {
21 $this->pdo = $pdo;
22 }
23
24 /**
25 * Insert a new project into the projects table
26 * @param string $projectName
27 * @return the id of the new project
28 */
29 public function insertProject($projectName) {
30 $sql = 'INSERT INTO projects(project_name) VALUES(:project_name)';
31 $stmt = $this->pdo->prepare($sql);
32 $stmt->bindValue(':project_name', $projectName);
33 $stmt->execute();
34
35 return $this->pdo->lastInsertId();
36 }
37
38 /**
39 * Insert a new task into the tasks table
40 * @param type $taskName
41 * @param type $startDate
42 * @param type $completedDate
43 * @param type $completed
44 * @param type $projectId
45 * @return int id of the inserted task
46 */
47 public function insertTask($taskName, $startDate, $completedDate, $completed, $projectId) {
48 $sql = 'INSERT INTO tasks(task_name,start_date,completed_date,completed,project_id) '
49 . 'VALUES(:task_name,:start_date,:completed_date,:completed,:project_id)';
50
51 $stmt = $this->pdo->prepare($sql);
52 $stmt->execute([
53 ':task_name' => $taskName,
54 ':start_date' => $startDate,
55 ':completed_date' => $completedDate,
56 ':completed' => $completed,
57 ':project_id' => $projectId,
58 ]);
59
60 return $this->pdo->lastInsertId();
61 }
62
63 }
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.
1 <?php
2
3 require 'vendor/autoload.php';
4
5 use App\SQLiteConnection;
6 use App\SQLiteInsert;
7
8 $pdo = (new SQLiteConnection())->connect();
9 $sqlite = new SQLiteInsert($pdo);
10
11 // insert a new project
12 $projectId = $sqlite->insertProject('PHP SQLite Demo');
13 // insert some tasks for the project
14 $sqlite->insertTask('Prepare the sample database schema', '2016-06-01', '2016-06-01', 1, $projectId);
15 $sqlite->insertTask('Create new tables ', '2016-05-01', null, 0, $projectId);
16 $sqlite->insertTask('Insert some sample data', '2016-05-01', '2016-06-02', 1, $projectId);
17
18 // insert a second project
19 $projectId = $sqlite->insertProject('Mastering SQLite');
20 // insert the tasks for the second project
21 $sqlite->insertTask('Go to sqlitetutorial.net', '2016-06-01', null, 0, $projectId);
22 $sqlite->insertTask('Read all the tutorials.', '2016-06-01', null, 0, $projectId);
23 $sqlite->insertTask('Use Try It page to practice the SQLite commands.', '2016-06-01', null, 0, $projectId);
24 $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.

Das könnte Ihnen auch gefallen