简体   繁体   中英

How to use SQLite3 in a portable fashion with PHP(5.4.x)

I am attracted by the grand claims of SQLite, too. However, just that like many other software, I have run into obsolete and not so easy to follow documentation and blogs by kind developers. Without more griping, here are the questions and details of the environment:

I have a web host provider running PHP 5.2.17, with installed SQLite version 2.8.17. phpinfo() output says (under pdo_sqlite section) that SQLite version is 3.3.7. At home, I have Apache server running PHP 5.4.10. phpinfo() works fine at both sites. The home laptop says (under sqlite3) the version is 3.7.7.1.

About the problem, I want to create a db with a few tables, and ftp it (the data file) back and forth between the home computer and the remote host. I am trying to create a PHP script to

  1. Create table if not exists legs ...
  2. Delete existing rows
  3. Insert a few new rows
  4. Read the table and print it out as Html table

Somehow I made up this file dbc1.php which runs fine on the remote server. A similar file dbc2.php fails on the "if not exists" clause, then on other statements, and cannot even read the database file if it was created by dbc1.php. The error message is:

file is encrypted or is not a database

If I move the file, dbc2.php will create it, but cannot fetch the inserted rows. The localhost version complains that

"Call to undefined function sqlite_libversion() in C:\\Web\\php\\dbcheck.php on line 14"

In my php.ini file, I have both extensions "php_pdo_sqlite.dll" and "php_sqlite3.dll" uncommented, and checked that both the dll files exist in the "ext" folder. What else do I need to do? The questions:

  1. Is the style in dbc1.php preferred over dbc2.php? Why or why not?
  2. Are the SQLite database files truly portable as claimed?
  3. May I have a pointer to PHP5-SQLite3 tutorial?
  4. Why do I get different version numbers from phpinfo() and sqlite_libversion()?

Thank you in advance for the responses. The 3 PHP files are below, although I hope to use 1 single file to perform the operations.

<html>

Testing SQLite with PHP

'; echo "SQLite version: " . sqlite_libversion() . '

';

    $db = new PDO('sqlite:dbname.db');      echo "[SQLite] DB opened<br/>";
    $db -> exec ($qc0);                     echo "[SQLite] table created<br/>";
    $db -> exec ($qr1);                     echo "[SQLite] Rows deleted<br/>";
    $db -> exec ($qi1); $db -> exec ($qi2);
    $db -> exec ($qi3); $db -> exec ($qi4); echo "[SQLite] Rows inserted<br/>";

    try {
            $rowsOfTable = $db -> query ($qr5);
            print "\t<table border='0' cellspacing='4' cellpadding='4'\n" .
                            "\t\t\tstyle='padding-top:20px;'>\n";
            print "\t\t<tr><td>Creation</td><td>Legs</td></tr>\n";
            foreach ($rowsOfTable as $row) {
                    echo "\t\t<tr>\n" .
                            "\t\t\t<td>" . $row['name'] . "</td>\n" .
                            "\t\t\t<td align='right'>" . $row['count'] . "</td>\n" .
                            "\t\t</tr>\n";
            }
            print "\t</table>\n";
    } catch (PDOException $ex) {
            die ($ex->getMessage());
    }

?>

Here is the file on the same Unix server that does not work:

<html>

Testing SQLite with PHP \\n"; echo "SQLite version: " . sqlite_libversion() . "
\\n";

    try {
            $db = new SQLiteDatabase('./dbname.db', 0644, $error);
            echo "SQLite DB opened<br/>\n";

            $db -> queryExec ($qc0, $error);        echo "[SQLite] table created<br/>\n";
            $db -> queryExec ($qr1, $error);        echo "[SQLite] Rows deleted<br/>";
            $db -> queryExec ($qi1, $error);
            $db -> queryExec ($qi2, $error);
            $db -> queryExec ($qi3, $error);
            $db -> queryExec ($qi4, $error);        echo "[SQLite] Rows inserted<br/><br/>";
            if ($tableFromDB = sqlite_query ($db, $qr5, SQLITE_BOTH, $error)) {
                    print "\t<table border='2' cellpadding='4'>\n";
                    while ($row = $tableFromDB -> fetch()) {
                            print "\t\t<tr>\n" .
                                    "\t\t\t<td>" . $row['name'] .
                                    "</td><td>" . $row['count'] . "</td>\n\t</tr>\n";
                    }
                    print "\t</table>\n";
            } else {
                    echo "Cannot query DB table: $error\n";
    }} catch (Exception $ex) {
            die ($error);
    }

?>

And finally, the code I am trying to run on the laptop:

<html>

Testing SQLite with PHP

phpinfo(); echo "PHP version: " . phpversion() . '
'; echo "SQLite version: " . sqlite_libversion() . '

';

    $db = new PDO('sqlite:dbname.db');      echo "[SQLite] DB opened<br/>";
    $db -> exec ($qc0);                     echo "[SQLite] table created<br/>";
    $db -> exec ($qr1);                     echo "[SQLite] Rows deleted<br/>";
    $db -> exec ($qi1); $db -> exec ($qi2);
    $db -> exec ($qi3); $db -> exec ($qi4); echo "[SQLite] Rows inserted<br/>";

    try {
            $rowsOfTable = $db -> query ($qr5);
            print "\t<table align='center' border='0' cellspacing='4' cellpadding='4'\n" .
                            "\t\t\tstyle='padding-top:20px;'>\n";
            print "\t\t<tr><td>Creation</td><td>Legs</td></tr>\n";
            foreach ($rowsOfTable as $row) {
                    echo "\t\t<tr>\n" .
                            "\t\t\t<td>" . $row['name'] . "</td>\n" .
                            "\t\t\t<td align='right'>" . $row['count'] . "</td>\n" .
                            "\t\t</tr>\n";
            }
            print "\t</table>\n";
    } catch (PDOException $ex) {
            die ($ex->getMessage());

} ?>

Happy New Year, everybody :)

Is the style in dbc1.php preferred over dbc2.php? Why or why not?

The classes and functions used in the second sample ("dbc2") are from the old SQLite 2 functions . SQLite2 is old , and the extension has actually been pushed off to PECL. You should not use it in modern code unless you need to read a SQLite2 database.

You should use either the SQLite3 class or PDO, as demonstrated in your first sample ("dbc1").

Are the SQLite database files truly portable as claimed?

Yes, but not between major versions. You can transplant an SQLite3 database file between any machine and platform and have it work correctly.

May I have a pointer to PHP5-SQLite3 tutorial?

Because your best bet will be using PDO, you really actually want a PDO tutorial. The only ones out there are for MySQL. We frequently recommend this tutorial for users coming from the mysql_ functions , but some people also recommend this other PDO tutorial . Read both, just use SQLite-compatible SQL instead of MySQL-flavored SQL.

The SQLite site has a comprehensive syntax section that details what it does and does not understand. For the most part, you can just ditch the backticks and have things work properly. SQLite also doesn't like bundling index creation with table creation, but neither do many other databases.

Why do I get different version numbers from phpinfo() and sqlite_libversion()?

This is an artifact of having both the old SQLite 2 extension installed while also having the SQLite 3 PDO extension installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM