简体   繁体   English

如何通过PHP(5.4.x)以可移植的方式使用SQLite3

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

I am attracted by the grand claims of SQLite, too. 我也被SQLite的宏伟主张所吸引。 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. 我有一个运行PHP 5.2.17,已安装SQLite版本2.8.17的Web主机提供程序。 phpinfo() output says (under pdo_sqlite section) that SQLite version is 3.3.7. phpinfo()输出表明(在pdo_sqlite部分下),SQLite版本为3.3.7。 At home, I have Apache server running PHP 5.4.10. 在家里,我有运行PHP 5.4.10的Apache服务器。 phpinfo() works fine at both sites. phpinfo()在两个站点上都能正常工作。 The home laptop says (under sqlite3) the version is 3.7.7.1. 家用笔记本电脑说(在sqlite3下)版本是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. 关于这个问题,我想创建一个带有几个表的数据库,然后在家用计算机和远程主机之间来回传送它(数据文件)ftp。 I am trying to create a PHP script to 我正在尝试创建一个PHP脚本来

  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 读取表格并将其打印为HTML表格

Somehow I made up this file dbc1.php which runs fine on the remote server. 我以某种方式组成了该文件dbc1.php,该文件可以在远程服务器上正常运行。 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. 类似的文件dbc2.php在“如果不存在”子句中失败,然后在其他语句中失败,并且如果它是由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. 如果我移动文件,dbc2.php将创建它,但是无法获取插入的行。 The localhost version complains that 本地主机版本抱怨

"Call to undefined function sqlite_libversion() in C:\\Web\\php\\dbcheck.php on line 14" “在第14行的C:\\ Web \\ php \\ dbcheck.php中调用未定义的函数sqlite_libversion()”

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. 在我的php.ini文件中,我没有对扩展名“ php_pdo_sqlite.dll”和“ php_sqlite3.dll”进行注释,并检查了这两个dll文件是否都在“ ext”文件夹中。 What else do I need to do? 我还需要做什么? The questions: 问题:

  1. Is the style in dbc1.php preferred over dbc2.php? 是dbc1.php中的样式优于dbc2.php吗? Why or why not? 为什么或者为什么不?
  2. Are the SQLite database files truly portable as claimed? SQLite数据库文件是否如所声称的那样真正可移植?
  3. May I have a pointer to PHP5-SQLite3 tutorial? 我可以指向PHP5-SQLite3教程吗?
  4. Why do I get different version numbers from phpinfo() and sqlite_libversion()? 为什么我从phpinfo()和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. 下面是3个PHP文件,尽管我希望使用1个单个文件来执行操作。

<html>

Testing SQLite with PHP 用PHP测试SQLite

'; '; echo "SQLite version: " . 回显“ SQLite版本:”。 sqlite_libversion() . 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: 这是同一台Unix服务器上不起作用的文件:

<html>

Testing SQLite with PHP \\n"; echo "SQLite version: " . sqlite_libversion() . " 使用PHP \\ n测试SQLite;“回显” SQLite版本:“。sqlite_libversion()。”
\\n"; \\ 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 用PHP测试SQLite

phpinfo(); phpinfo(); echo "PHP version: " . 回显“ PHP版本:”。 phpversion() . phpversion()。 ' '
'; '; echo "SQLite version: " . 回显“ SQLite版本:”。 sqlite_libversion() . 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? 是dbc1.php中的样式优于dbc2.php吗? Why or why not? 为什么或者为什么不?

The classes and functions used in the second sample ("dbc2") are from the old SQLite 2 functions . 第二个示例(“ dbc2”)中使用的类和函数来自旧的SQLite 2函数 SQLite2 is old , and the extension has actually been pushed off to PECL. SQLite2很 ,扩展名实际上已推到PECL。 You should not use it in modern code unless you need to read a SQLite2 database. 除非需要读取SQLite2数据库,否则不应在现代代码中使用它。

You should use either the SQLite3 class or PDO, as demonstrated in your first sample ("dbc1"). 如第一个示例(“ dbc1”)所示,您应该使用SQLite3类或PDO。

Are the SQLite database files truly portable as claimed? SQLite数据库文件是否如所声称的那样真正可移植?

Yes, but not between major versions. 是的,但不是在主要版本之间。 You can transplant an SQLite3 database file between any machine and platform and have it work correctly. 您可以在任何计算机和平台之间移植SQLite3数据库文件,并使其正常工作。

May I have a pointer to PHP5-SQLite3 tutorial? 我可以指向PHP5-SQLite3教程吗?

Because your best bet will be using PDO, you really actually want a PDO tutorial. 因为最好的选择是使用PDO,所以您实际上确实需要PDO教程。 The only ones out there are for MySQL. 仅有的是MySQL。 We frequently recommend this tutorial for users coming from the mysql_ functions , but some people also recommend this other PDO tutorial . 我们经常向来自mysql_函数的用户推荐本教程 ,但是有些人也推荐其他PDO教程 Read both, just use SQLite-compatible SQL instead of MySQL-flavored SQL. 两者都读,只使用兼容SQLite的SQL而不是MySQL风格的SQL。

The SQLite site has a comprehensive syntax section that details what it does and does not understand. SQLite网站有一个综合的语法部分 ,详细介绍了它的作用和不理解的地方。 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. SQLite还不喜欢将索引创建与表创建捆绑在一起,但是许多其他数据库也不喜欢。

Why do I get different version numbers from phpinfo() and sqlite_libversion()? 为什么我从phpinfo()和sqlite_libversion()获得不同的版本号?

This is an artifact of having both the old SQLite 2 extension installed while also having the SQLite 3 PDO extension installed. 这是既安装了旧的SQLite 2扩展,又安装了SQLite 3 PDO扩展的人工产物。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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