| This article introduces a class used to operate sqlite data in php programming. here we will share the code. if you need it, please refer to it.
Provides a PHP SQLite class:
Any database file name // create database "title =" database "> database table. $ DB-> query ("create table test (id integer primary key, title varchar (50 ))"); // add the data $ DB-> query ("insert into test (title) values ('hut ')"); $ DB-> query ("insert into test (title) values ('Blue Rain') "); $ DB-> query (" insert into test (title) values ('ajand ')"); $ DB-> query ("insert into test (title) values ('sjolzy ')"); // 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) {// Retrieve 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 ($ SQL)-> fe TchAll ();} function RecordCount ($ SQL) {return count ($ this-> RecordArray ($ SQL);} function RecordLastID () {return $ this-> connection-> lastInsertId () ;}}?>
Appendix, related PHP configuration instructions:
1. first test whether PHP can connect to the sqlite database "title =" database "> Database: Create a PHP file.
Whether the test file runs normally.
If the sqlite module fails to be loaded normally, the following error may occur: Fatal error: Call to undefined function sqlite_open () in C: \ Apache \ Apache2 \ htdocs \ test. php on line 2.
Solution: Open the php. ini file, remove the semicolon before the following three lines, that is, remove the comment to make the configuration section take effect: ; Extension = php_sqlite.dll; extension = php_pdo.dll; extension = php_pdo_sqlite.dll
Restart apache or nginx to make the configuration take effect. |