为什么我可以在PHP的SQLite数据库上使用SELECT但不能使用INSERT? [英] Why can I use SELECT but not INSERT on a SQLite database in PHP?

查看:101
本文介绍了为什么我可以在PHP的SQLite数据库上使用SELECT但不能使用INSERT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够从PHP中的SQLite数据库检索信息,但无法写入信息.例如,此代码可以正常工作:

I am able to retrieve information from a SQLite database in PHP, but not write to it. For example, this code works perfectly fine:

$db = new PDO("sqlite:foo.db");
$rowCount = $db->query("SELECT COUNT(*) FROM tblname;")->fetchColumn(0);
echo $rowCount; // works as it should

但是,以下代码段导致出现无法打开数据库文件"错误:

However, the following snippet results in an, 'unable to open database file' error:

$db = new PDO("sqlite:foo.db");
$db->exec("INSERT INTO tblname VALUES(NULL, 'val1', 'val2');") or die(print_r($db->errorInfo(), true));

当我从命令行而不是在PHP中执行上述INSERT时,我得到了预期的结果(tblname中的新行):

When I perform the above INSERT from the command line rather than in PHP, I get the expected results (a new row in tblname):

$ sqlite3 foo.db
sqlite> INSERT INTO tblname VALUES(NULL, 'val1', 'val2');

这是我第一次使用PDO(或SQLite),因此将不胜感激.

This is my first time using PDOs (or SQLite for that matter) so any help would be greatly appreciated.

推荐答案

检查运行PHP脚本的用户是否具有写foo.db的权限.

Check that the user running the PHP script has permission to write foo.db.

(从内存中,如果我错了,请纠正我)用户将需要在包含foo.db的目录上具有写许可权,否则SQLite3会抛出不那么有用的错误消息.

Also (from memory, correct me if I'm wrong) the user will need write permission on the directory containing foo.db, or else SQLite3 will throw a not-so-useful error message.

这篇关于为什么我可以在PHP的SQLite数据库上使用SELECT但不能使用INSERT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆