Introduction to SQLite
SQLite is a light-weight database, an associated database management system that complies with ACID. Its design goal is embedded, and it has been used in many embedded products. It occupies very low resources. In embedded devices, it may only require a few hundred K of memory.
It can support mainstream operating systems such as Windows/Linux/Unix, and can also be combined with many programming languages, such as Tcl, PHP, Java, C++, .Net, etc., as well as ODBC interfaces. Compared with Mysql and PostgreSQL, the two open source world-renowned database management systems, its processing speed is faster than them.
Use PHP to connect SQLite to create a table, and use INSERT and SELECT statements to operate the SQLITE database.
Before using SQLite, we need to make sure that sqlite and pdo configurations are enabled in php.ini
Open the PHP.INI file and type the following extension:
The code copy is as follows:
extension=php_pdo.dll
extension=php_pdo_sqlite.dll
extension=php_sqlite.dll
The sqlite_open command opens a database file.
Create if there is no file.
sqlite_query can execute SQL statements.
Create a table and insert data.
sqlite_unbuffered_query issues a SELECT statement.
Loop and display the results.
unable to open a temporary database file for storing temporary tables
The temporary database file that stores temporary tables cannot be opened. In Windows environment, if the above error occurs,
Please use putenv("TMP=C:/temp"); to specify a temporary folder.
For details, please see the code:
<?php //Temporary directory is in a Windows environment. If the above error occurs, please use putenv("TMP=C:/temp"); to specify the temporary folder. //putenv("TMP=C:/temp"); //Open the 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 the SQL statement $res = sqlite_unbuffered_query($db, $sql); //Show the result while ($item = sqlite_fetch_array($res, SQLITE_ASSOC)) {print "ID:".$item["id"] ."NAME:".$item["name"];print "<BR>";}; //Close the database sqlite_close($db); } else {print $sqliteerror;}?>PHP+SQLite database operation tutorial and examples
<?php //Set the maximum execution time of the script 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 connect to SQlite files: ',$db_name,'<br />'; } //Create data table: MD5 password table sqlite_query($db, "CREATE TABLE md5 (s int(4) PRIMARY KEY,d varchar(32))"); //Insert record $s = 0; while($s <= 999999){ $d = md5($s); sqlite_query($db, "INSERT INTO md5 VALUES ($s,'{$d}')"); $s++; } //Search 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>'; //Close SQLite connection sqlite_close($db);?>PHP Reading SQLite Introduction
<?php//Open the sqlite database//$db = @sqlite_open("MM.sqlite", 0666, $error); // Not supported //$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')");//Search all records $result = $db->query('select BottleEncryptUsrName from BottleTable4');//Print the obtained result foreach($result as $row){echo $row[0];echo "<br>";}?>