Php+sqlite Database Operation Example Detailed

Source: Internet
Author: User
This article mainly introduces the method of Php+sqlite database operation, simple analysis of the functions of SQLite database and related operation skills, including creating, opening, inserting, retrieving and error hints, and so on, need friends can refer to the next

Specific as follows:

SQLite is a lightweight database, it is designed to be embedded, and has been used in many embedded products, it occupies a very low resource, in the embedded device, may only need hundreds of K of memory is enough. It can support Windows/linux/unix and so on mainstream operating system, and can be combined with many programming languages, such as Tcl, PHP, Java, and ODBC interface, also compared to MySQL, PostgreSQL, the two open source world-renowned database management system, is processing faster than they do.

PHP 5 is no longer the default support for MySQL, but the default support SQLite, it can be seen how much influence, so if you want to do sqlite PHP development, it is recommended that you use PHP 5.0.0 or later.

Here is an example of using SQLite

You can start by creating a new upload.db empty file in the directory.

<?phpdefine ("LN", __line__);//Line number define ("FL", __file__);//Current file define ("Debug", 0);//debug Switch $db_name = "upload.db";// Create the database file with the empty 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 () UNIQUE, make_time integer)") {Exit (Error_code ( -3, LN));} Insert a 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 out if (!) ( $result = Sqlite_query ($db, "select * from uploads")) {exit (Error_code ( -5, LN));} Gets the retrieved data and displays the while ($array = Sqlite_fetch_array ($result)) {echo "ID:". $array [file_name]. " <br>: ". $array [Make_time];} /* Error message 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: $errmsg = "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>line:". LN. " &LT;BR&GT;MESG: ". $errmsg. ""; if (! $debug) {($m = $errmsg);} return $m;}? >

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.