Simple PHP-implemented SQLite database class and usage example, sqlite example

Source: Internet
Author: User

Simple PHP-implemented SQLite database class and usage example, sqlite example

This article provides examples of simple PHP operations on SQLite database classes and usage. We will share this with you for your reference. The details are as follows:

SQLite is a lightweight database and an ACID-compliant associated database management system. It is designed to be embedded and has been used in many embedded products, it occupies very low resources. In embedded devices, it may only need several hundred KB of memory. It supports mainstream operating systems such as Windows, Linux, and Unix, and can be combined with many programming languages, such as Tcl, PHP, Java, and ODBC interfaces, similar to MySQL and PostgreSQL, the two world-renowned open-source database management systems, the processing speed is faster than that of them.

Here we provide you with a simple PHP operation SQLite class:

<? Php/*** // application example require_once ('cls _ sqlite. php '); // create an instance $ DB = new SQLite ('blog. db'); // The database file name. // create a database table. $ DB-> query ("create table test (id integer primary key, title varchar (50 ))"); // Add data $ DB-> query ("insert into test (title) values ('kimchi ')"); $ DB-> query ("insert into test (title) values ('Blue rain') "); $ DB-> query (" insert into test (title) values ('ajand ')"); $ DB-> query ("insert into test (title) values ('snow blue sky ')"); // read data print_r ($ DB-> getlist ('select * from test order by id desc ')); // update data $ DB-> query ('Update test set title =" Three major "where id = 9'); ***/class SQLite {function _ construct ($ file) {try {$ this-> connection = new PDO ('sqlite: '. $ file);} catch (PDOException $ e) {try {$ this-> connection = new PDO ('sqlite2 :'. $ file);} catch (PDOException $ e) {exit ('error! ') ;}} Function _ destruct () {$ this-> connection = null;} function query ($ SQL) // run SQL directly, can be used to update or delete data {return $ this-> connection-> query ($ SQL);} function getlist ($ SQL) // obtain the record list {$ recordlist = array (); foreach ($ this-> query ($ SQL) as $ rstmp) {$ recordlist [] = $ rstmp ;} return $ recordlist;} function Execute ($ SQL) {return $ this-> query ($ SQL)-> fetch ();} function RecordArray ($ SQL) {return $ this-> query ($ sq L)-> fetchAll ();} function RecordCount ($ SQL) {return count ($ this-> RecordArray ($ SQL);} function RecordLastID () {return $ this-> connection-> lastInsertId () ;}}?>

Related PHP configuration instructions:

1. First test whether PHP can connect to the sqlite database:

Create a PHP File

<?php$conn = sqlite_open('test.db');?>

Test whether the file runs normally.

If the sqlite module is not properly loaded, the following error may occur:

Fatal error: Call to undefined function sqlite_open () in C: \ Apache \ Apache2 \ htdocs \ test. php on line 2

The solution is as follows:

2. Open the php. ini file and delete the semicolon before the following three lines:

;extension=php_sqlite.dll;extension=php_pdo.dll;extension=php_pdo_sqlite.dll

Restart the web Server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.