简体   繁体   English

PDO PHP SQLite3数据库连接常规错误14

[英]PDO PHP SQLite3 database connection General Error 14

I have 2 sqlite3.x databases (from an iPhone), one from a game app, and the other is the address book. 我有2个sqlite3.x数据库(来自iPhone),一个来自游戏应用程序,另一个是通讯录。

I have logging and display errors 'on' in the php conf. 我在php conf中记录日志并显示错误“ on”。

Using PHP/PDO I can display the fields and values from the game database, including image blobs which I catch with my results page and render with "img src='data:...'. I'm using htmlentities on other fields to catch things like plist binary blobs. 使用PHP / PDO,我可以显示游戏数据库中的字段和值,包括我在结果页中捕获的图像斑点,并使用“ img src ='data:...”进行渲染。我在其他字段上使用htmlentities来捕获plist二进制blob之类的东西。

I'm using a perl script to generate the first page, and the sample data is obtained using a system call to sqlite3. 我正在使用perl脚本生成第一页,并且使用对sqlite3的系统调用来获取示例数据。 The script is doing a recursive search through a directory provided as a cli argument. 该脚本正在通过作为cli参数提供的目录进行递归搜索。

I basically get the first record from each table within a database (as a data sample), give the user options on which fields they want to further interrogate, and pass those options to a form handler which executes the query against the target table, and getting data from the chosen columns. 我基本上是从数据库中的每个表中获取第一条记录(作为数据样本),为用户提供他们想要进一步查询的字段的选项,并将这些选项传递给对目标表执行查询的表单处理程序,以及从所选列中获取数据。

However...I get a "unable to open database" general error when trying to connect to the addressbook database. 但是...尝试连接到地址簿数据库时,出现“无法打开数据库”的一般错误。

I have checked the database permissions, ownership, put both in the same directory, made them fully open to the world..everything I can think of, and still no joy. 我已经检查了数据库的权限,所有权,将它们都放在同一个目录中,使它们完全对世界开放。

What's really odd is that I can query all databases quite happily from the commandline with sqlite3, but get this error when using PDO. 真正奇怪的是,我可以使用sqlite3从命令行非常愉快地查询所有数据库,但是使用PDO时会出现此错误。

The whole things trips up at the following: 整个过程如下:

($db and $strSQL vals passed from previous page)

$dbh = new PDO('sqlite:'.$db);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$result = $dbh->query($strSQL);

Even if I select a simple primary key column from the table, I get the error, so I'm pretty sure it's not a data handling problem. 即使我从表中选择一个简单的主键列,也会收到错误,因此我可以肯定这不是数据处理问题。 I've even tried this same script against Firefox and Chrome databases...no problems at all. 我什至尝试对Firefox和Chrome数据库使用相同的脚本...完全没有问题。

Would really appreciate any ideas/pointers/suggestions. 真的很感谢任何想法/指针/建议。 This one is doing my head in! 这是我的脑袋!

Cheers. 干杯。

OK...So it seems that despite the fact the database files themselves were wide open (chmod 777), the directory where they sit has to be writeable! OK ...所以看来,尽管事实上数据库文件本身是完全打开的(chmod 777),但它们所在的目录必须是可写的! Still not sure how sqlite from the commandline gets away with not requiring a writable directory, but you certainly need it for PHP/PDO queries. 仍不确定不需要通过可写目录从命令行获得sqlite如何,但是对于PHP / PDO查询,您肯定需要它。

The databases that were not showing results have the Write-Ahead Logging (WAL) bits set (bytes 18 and 19 of the file header), and the directory needs to be writeable so SQLite can create the WAL version of the database being queried. 未显示结果的数据库设置了预写日志记录(WAL)位(文件头的字节18和19),并且该目录需要可写,以便SQLite可以创建要查询的数据库的WAL版本。 Bytes 18 and 19 are set to 0x01 and 0x01 for non-WAL dbs, and 0x02 0x02 for WAL enabled databases. 对于非WAL数据库,字节18和19设置为0x01和0x01,对于启用WAL的数据库设置为0x02 0x02。

What gave it away?...the sudden appearance of copies of my addressbook database file with "-wal" and "-shm" appended to the end of the filename when I re-ran the query and happened to have moved the DBs into a writeable directory for testing. 是什么让它消失了?...当我重新运行查询并碰巧将数据库移入数据库时​​,突然出现了地址簿数据库文件的副本,在文件名的末尾附加了“ -wal”和“ -shm”用于测试的可写目录。

Go figure! 去搞清楚!

PS.. Interestingly enough, every time the journal mode is changed (using a db query "pragma journal_mode=[wal|off]"), a 2 byte counter at bytes 94 and 95 increments by one. PS ..有趣的是,每次更改日志模式(使用db查询“ pragma journal_mode = [wal | off]”)时,位于94和95字节的2字节计数器将递增1。 Noted this when checking the hash values of the db when flipping between modes, and could not get the same 2 hashes. 在模式之间进行切换时检查db的哈希值时,请注意这一点,并且无法获得相同的2个哈希值。 I did a xxd on a before and after database, piped the output to txt files, then diff'd the 2 txt files...and there were these counter bytes. 我在数据库之前和之后进行了xxd处理,将输出通过管道传输到txt文件,然后将2个txt文件进行比较...然后有这些计数器字节。

Based on the error message "Unable to open Database", it's unable to access the sqlite file. 根据错误消息“无法打开数据库”,它无法访问sqlite文件。 I am sure you have checked your "$db" variable from previous page. 我确定您已经检查了上一页中的“ $ db”变量。

It's difficult to give you a definite answer. 很难给您一个明确的答案。 Can you try without PDO, just to check wether there is an issue with sqlite file or the PHP code. 您是否可以在没有PDO的情况下尝试,仅检查sqlite文件或PHP代码是否存在问题。

Post your results and lets see. 发布结果,让我们看看。

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

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