Php: getting started with sqlite database instance code _ javascript skills

Source: Internet
Author: User
This article introduces a simple example for php to read the sqlite database. For more information about how to operate the sqlite getting started instance in php programming, see SQLite Introduction

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, C ++, and ,. and ODBC interfaces, which are faster than Mysql and PostgreSQL, the world-renowned open-source database management systems.

Use PHP to connect to SQLite to create a table and use INSERT and SELECT statements to operate the SQLITE database.

Before using SQLite, make sure that the sqlite and pdo configurations are enabled in php. ini.

Open the PHP. ini file and perform the following extensions:

The Code is as follows:


Extension = php_pdo.dll
Extension = php_pdo_sqlite.dll
Extension = php_sqlite.dll

The sqlite_open command is used to open a database file.
If no file exists, it is created.

Sqlite_query can execute SQL statements.
Create a table and insert data.

Sqlite_unbuffered_query issues a SELECT statement.

Loop and display results.

Unable to open a temporary database file for storing temporary tables
The temporary database file storing temporary tables cannot be opened. In Windows, if the above error occurs,
Use putenv ("TMP = C:/temp"); to specify a Temporary Folder.

For details, see the code:

<? Php // The temporary directory is in Windows. If the preceding error occurs, 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 a table sqlite_query ($ db," create table user (id integer primary key, name text );"); // INSERT statement $ SQL = "insert into user values (NULL, 'name')"; // execute the 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 ); // display the result while ($ item = sqlite_fetch_array ($ res, SQLITE_ASSOC) {print "ID :". $ item ["id"]. "NAME :". $ item ["name"]; print"
";}; // Close the database sqlite_close ($ db) ;}else {print $ sqliteerror ;}?>

PHP + SQLite database operation tutorial and example

<? Php // set the maximum execution time of the script set_time_limit (0); // sqlite database file name $ db_name = 'md5. db'; // open the sqlite database $ db = sqlite_open ($ db_name); // Exception Handling if (! $ Db) {echo 'cannot connect to the SQlite file:', $ db_name ,'
';} Else {echo' successfully connected the SQlite file: ', $ db_name ,'
';} // CREATE a 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 ++;} // retrieve all records $ result = sqlite_query ($ db, 'select * FROM md5'); echo'
';    while ($row = sqlite_fetch_array($result, SQLITE_BOTH)) {        echo 'Md5:',$row['d'],' Src:',$row['s'], '
'; } echo '
'; // Close the SQLite connection sqlite_close ($ db);?>

Php read sqlite entry Edition

<? 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 named 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 the obtained result foreach ($ result as $ row) {echo $ row [0]; echo"
";}?>
Related Article

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.