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
This article describes the methods of Php+sqlite database operations. Share to everyone for your reference, 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);//row number define ("FL", file);//Current define ("Debug", 0);//debug Switch $db_name = "upload.db";//CREATE Database file, The contents of the file are 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. " <BR>MESG: ". $errmsg. ""; if (! $debug) {($m = $errmsg);} return $m;}? >