PHP PDO SQLite Example – Insert, Read, Search

 

In this lesson you are going to learn the below stuffs:

1. How to Create and connect to a database in SQLite using PDO.
2. How Create a table in SQLite
3. How to insert values into the SQLite table
4. How to read all records from SQLite table
5. How to search a record in SQLite table

All the above operation using PHP PDO driver not the old mysql driver. The beauty of using PDO driver is, you can use the same PHP script for SQLite database as well as MySQL database, all you have do is change the PDO connection strings respectively.

Let’s write the connection first (connection.php):

Like I said you can simple switch over the database easily if you use PDO driver, In the above code I have given connection for both SQLite and MySQL, In case if SQLite is not working or not installed in your server you can always use MySQL, just uncomment the MySQL part and comment the SQLite connection. But keep in mind when you open a connection for SQLite, the database will be created automatically but in MySQL you have to create it manually.

1. Okay, now create a table first (create-pdo-sqlite.php):

 

2. Insert Records (insert-pdo-sqlite.php)

 

3. Read all records from SQLite database (read-pdo-sqlite.php):

 

4. Read a particular record, basically a search option (search-pdo-sqlite.php):

I hope this will give you a clear idea about handling SQLite or MySQL database using PDO driver.

For you convenience, here is the demo and download:

demo download

 

 

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *