How to connect to SQLite database using PDO

WBOY
Release: 2023-07-29 14:58:01
Original
1192 people have browsed it

How to connect to a SQLite database using PDO

When developing web applications, the database is an indispensable technology. SQLite is a lightweight embedded database engine that is widely used in a variety of applications. This article will introduce how to use PHP's PDO (PHP Data Object) extension to connect to a SQLite database and provide some code examples.

PDO is a unified interface for PHP used to access different databases. Using PDO makes it easy to switch between different databases without modifying too much code. SQLite is one of the databases supported by PDO, let's see how to connect to a SQLite database using PDO.

First, we need to ensure that the SQLite database has been installed on the server and the PDO extension has been enabled. If it is not installed, please install and configure it according to your own server environment.

Next, we need to create a SQLite database file that will store our data. We can use SQLite's command line tools or other SQLite management tools to create an empty database.

Now, we can start writing PHP code to connect to the SQLite database. Here is a simple example:

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 执行查询语句 $statement = $pdo->query("SELECT * FROM users"); // 遍历结果集 while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { echo "ID: " . $row['id'] . ", Name: " . $row['name'] . "
"; } } catch(PDOException $e) { // 处理异常 echo "连接到数据库时发生错误: " . $e->getMessage(); }
Copy after login

In the above example, we first create a PDO object using thenew PDO()function and pass it the path to the SQLite database file as a parameter . Next, we set the first parameter of thesetAttribute()method to PDO::ATTR_ERRMODE and the second parameter to PDO::ERRMODE_EXCEPTION to throw an exception when an error occurs.

Then, we use thequery()method to execute a simple query statement and assign the result to a variable. Finally, we use thefetch()method to traverse the result set and print out the data for each row.

It should be noted that the path/path/to/database.dbin the above example should be replaced with the actual database file path.

In addition to executing query statements, we can also use PDO objects to perform other database operations, such as inserting, updating, and deleting data. The following is a sample code:

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 插入数据 $pdo->exec("INSERT INTO users (name) VALUES ('John')"); // 更新数据 $pdo->exec("UPDATE users SET name = 'Jane' WHERE id = 1"); // 删除数据 $pdo->exec("DELETE FROM users WHERE id = 1"); } catch(PDOException $e) { // 处理异常 echo "连接到数据库时发生错误: " . $e->getMessage(); }
Copy after login

In the above example, we used theexec()method to execute INSERT, UPDATE and DELETE statements to insert, update and delete data.

Summary
This article explains how to use PDO to connect to a SQLite database and provides some code examples. Connecting to a SQLite database using PDO is very simple, requiring only a few lines of code to complete the connection and database operations. Using PDO to connect to a SQLite database is a common way to develop web applications, which can greatly improve development efficiency. Hope this article can be helpful to you!

The above is the detailed content of How to connect to SQLite database using PDO. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!