PHP + sqlite database operation example (Create/Open/insert/search), sqlite example

Source: Internet
Author: User

PHP + sqlite database operation example (Create/Open/insert/search), sqlite example

This article describes how to operate a PHP + sqlite database. We will share this with you for your reference. The details are as follows:

SQLite is a lightweight database. It is designed to be embedded and has been used in many embedded products. It occupies low resources, in an embedded device, only a few hundred KB of memory is required. 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.

In PHP 5, Mysql is no longer supported by default, but SQLite is supported by default. This shows how influential it is. If you want to develop PHP for SQLite, we recommend that you use PHP 5.0.0 or later.

The following is an example of using sqlite.

First, you can create an empty upload. db file in the directory.

<? Phpdefine ("LN", _ LINE _); // LINE number define ("FL", _ FILE _); // define ("DEBUG ", 0); // debug switch $ db_name = "upload. db "; // create a database file. The file content is blank. if (! File_exists ($ db_name) {if (! ($ Fp = fopen ($ db_name, "w +") {exit (error_code (-1, LN) ;}fclose ($ fp );} // open the database file if (! ($ Db = sqlite_open ($ db_name) {exit (error_code (-2, LN);} // generate the data table structure if (! Sqlite_query ($ db, "drop table uploads") {exit (error_code (-3, LN);} if (! Sqlite_query ($ db, "create table uploads (id integer primary key, file_name varchar (200) UNIQUE, make_time integer)") {exit (error_code (-3, LN);} // insert a piece of data if (! Sqlite_query ($ db, "insert into uploads (file_name, make_time) VALUES ('upload/111. data ','". time (). "')") {exit (error_code (-4, LN);} // retrieve the data if (! ($ Result = sqlite_query ($ db, "SELECT * FROM uploads") {exit (error_code (-5, LN ));} // retrieve the Retrieved Data and display while ($ array = sqlite_fetch_array ($ result) {echo "ID :". $ array [file_name]. "<br> :". $ array [make_time];}/* error code function */function error_code ($ code, $ line_num, $ debug = DEBUG) {if ($ code <-6 | $ code>-1) {return false;} switch ($ code) {case-1: $ errmsg = "Create database file error. "; break; case-2: $ err Msg = "Open sqlite database file failed. "; break; case-3: $ errmsg =" Create table failed, table already exist. "; break; case-4: $ errmsg =" Insert data failed. "; break; case-5: $ errmsg =" Query database data failed. "; break; case-6: $ errmsg =" Fetch data failed. "; break; case-7: $ errmsg =" "; break; default: $ errmsg =" Unknown error. ";}$ m =" <B> [Error] </B> <br> File :". basename (FL ). "<br> L Ine: ". LN." <br> Mesg: ". $ errmsg." "; if (! $ Debug) {($ m = $ errmsg);} return $ m ;}?>

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.