Php+sqlite Database Operations Example (create/Open/insert/retrieve) _php tips

Source: Internet
Author: User
Tags php define sqlite sqlite database

The example of this article describes the method of Php+sqlite database operation. Share to everyone for your reference, specific as follows:

SQLite is a lightweight database, its design goal is 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 so on mainstream operating system, simultaneously can with many programming language combination, for instance TCL, PHP, Java and so on, also has the ODBC interface, similarly compared to MySQL, PostgreSQL the two open source world's most famous database management system, it processing faster than them.

PHP 5 no longer defaults to support MySQL, but the default support SQLite, see how powerful it is, so if you want to do sqlite PHP development, it is recommended that you use PHP 5.0.0 version.

Here is an example of using SQLite

You can first create a new upload.db empty file under the directory

<?php define ("LN", __line__)//Line number define ("FL", __file__);//Current file define ("Debug", 0);//debug switch $db _name = "upload.db"; Creates a database file with an 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 Data table structure if (!sqlite_query ($db, DROP TABLE uploads)) {EX
It (Error_code ( -3, LN)); !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 (' Up Load/111.data ', ' ". Time ()." ') ")
{Exit (Error_code ( -4, LN));} Retrieve the data out of the IF (!) ( $result = Sqlite_query ($db, "SELECT * from uploads"))) {exit (Error_code ( -5, LN);}//Get retrieve data and display while ($array = Sqlite_f Etch_array ($result)) {echo "ID:". $array [file_name]. " <br>: ".
$array [Make_time]; /* Error message Code functions/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;

 }?>

More about PHP Interested readers can view the site topics: "PHP based on PDO Operation Database Skills Summary", "PHP+MONGODB Database Operation Skills Encyclopedia", "PHP object-oriented Programming Program Introduction", "PHP string (String) Usage Summary", " Introduction to PHP+MYSQL Database operations and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

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.