This article introduces a simple example of PHP reading SQLite database, PHP programming to operate the example of SQLite, the need for friends can refer to the following
About SQLite
SQLite is a lightweight database, is to comply with acid-related 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, may only need hundreds of K of memory is enough.
It can support Windows/linux/unix and other mainstream operating systems, and can be combined with many programming languages, such as Tcl, PHP, Java, C + +,. NET, as well as the ODBC interface, it is faster than the two open source world-famous database management systems, such as Mysql and PostgreSQL.
Single-Use the PHP connection to SQLite to create the table and use the INSERT and SELECT statements to manipulate the SQLite database.
Before using SQLite, we want to make sure that the SQLite and PDO configurations are enabled in PHP.ini
Open a php.ini file and lay the following extensions:
The code is as follows:
Extension=php_pdo.dllextension=php_pdo_sqlite.dllextension=php_sqlite.dll
The Sqlite_open command is to open a database file.
If no files are created.
Sqlite_query can execute SQL statements.
Create a table and insert data.
Sqlite_unbuffered_query issues a SELECT statement.
Loops and displays the results.
Unable to open a temporary database file for storing temporary tables
You cannot open a temporary database file that stores temporary tables, in a Windows environment, if the above error occurs,
Please use putenv ("Tmp=c:/temp") and specify a temporary folder.
For details, see the code:
<?php//Temp directory in a Windows environment, if the above error occurs, use Putenv ("Tmp=c:/temp") and specify a temporary folder. Putenv ("Tmp=c:/temp"); Open database if ($db = Sqlite_open ("Test.db", 0666, $sqliteerror)) {//CREATE TABLE Sqlite_query ($db, "CREATE TABLE user (ID integer primary key,name text); "); Insert statement $sql = "INSERT into user values (NULL, ' name ')"; Execute SQL statement $res = sqlite_query ($db, $sql); SELECT statement $sql = "SELECT * from user order BY id desc limit 20"; Execute SQL statement $res = sqlite_unbuffered_query ($db, $sql); Displays the result while ($item = Sqlite_fetch_array ($res, Sqlite_assoc)) {print "ID:". $item ["id"]. " Name: ". $item [" name "];p rint" <BR> ";}; Close Database Sqlite_close ($DB); } else {print $sqliteerror;}? >
Php+sqlite Database operation Tutorials and examples
<?php //Set script maximum execution time set_time_limit (0); SQLite database file name $db _name = ' md5.db '; Open SQLite database $db = Sqlite_open ($db _name); Exception handling if (! $db) { echo ' cannot connect to SQLite files: ', $db _name, ' <br/> '; } else{ Echo ' successfully connected SQLite file: ', $db _name, ' <br/> '; } Create data table: MD5 password table sqlite_query ($db, "Create TABLE MD5 (s int (4) PRIMARY key,d varchar (+))"); Insert record $s = 0; while ($s <= 999999) { $d = MD5 ($s); Sqlite_query ($db, "INSERT into MD5 VALUES ($s, ' {$d} ') '); $s + +; } Retrieve all records $result = Sqlite_query ($db, ' SELECT * from MD5 '); Echo ' <pre> '; while ($row = Sqlite_fetch_array ($result, Sqlite_both)) { echo ' Md5: ', $row [' d '], ' SRC: ', $row [' s '], ' <br/> '; } Echo ' </pre> '; Turn off SQLite connection sqlite_close ($db);? >
PHP Read the starter version of SQLite
<?php//open SQLite database//$db = @sqlite_open ("Mm.sqlite", 0666, $error);//does not support//$ db = new PDO (' sqlite:MM.sqlite ');//exception handling if (! $db) die ("Connection sqlite failed.\n");//Add a database called Foo//@sqlite_query ($ DB, "CREATE TABLE foo (bar varchar (10))");//Insert a record//@sqlite_query ($db, "INSERT into Foo VALUES (' Fnord ')");//Retrieve All records $ result = $db->query (' Select Bottleencryptusrname from BottleTable4 ');//Print Get result foreach ($result as $row) {echo $row [0] ; echo "<br>";}?