PDO SQLite查询零结果问题
我环顾四周,但似乎找不到任何有关此的信息。我不确定这是我的代码的问题还是内存 SQLite 数据库和 PDO 的已知问题。
基本上,在将一行插入内存 SQLite 数据库表后,我希望与插入的项目不匹配的查询返回零行。但是,以下代码给出了一行 (false),但没有实际的 PDO 错误代码。
<?php
// Create the DB
$dbh = new PDO('sqlite::memory:');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Data we'll be using
$name = 'Entry';
// Create DB table
$dbh->query('
CREATE TABLE
Test
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(50) NOT NULL
)
');
// Insert data
$stmt = $dbh->prepare('
INSERT INTO
Test
(
name
)
VALUES
(
:name
)
');
$stmt->bindParam(':name', $name, PDO::PARAM_STR, 50);
$stmt->execute();
// Check data has actually been inserted
$entries = $dbh->query('
SELECT
*
FROM
Test
')->fetchAll(PDO::FETCH_ASSOC);
var_dump($entries);
// Query DB for non-existent items
$stmt = $dbh->prepare('
SELECT
*
FROM
Test
WHERE
name != :name
');
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->execute();
// How many rows returned
echo $stmt->rowCount();
// Actual data returned
var_dump($stmt->fetch(PDO::FETCH_ASSOC));
?>
我已经设法通过一些黑客技术解决了这个问题,但最好不必这样做:
<?php
echo (
(0 == $stmt->rowCount()) ||
(
(1 == $stmt->rowCount()) &&
(false === (($row = $stmt->fetch(PDO::FETCH_ASSOC)))) &&
('0000' == array_pop($dbh->errorInfo()))
)
) ? 'true' : 'false';
?>
任何人都可以帮助或指出我可能犯的任何明显错误吗?
I've had a look around but can't seem to find any information on this. I'm not sure if it's an issue with my code or a known issue with in-memory SQLite databases and PDO.
Basically, after inserting a single row into an in-memory SQLite database table, I'd expect that a query that doesn't match the inserted item to return zero rows. However, the following code gives a single row (false) but no actual PDO error code.
<?php
// Create the DB
$dbh = new PDO('sqlite::memory:');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Data we'll be using
$name = 'Entry';
// Create DB table
$dbh->query('
CREATE TABLE
Test
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(50) NOT NULL
)
');
// Insert data
$stmt = $dbh->prepare('
INSERT INTO
Test
(
name
)
VALUES
(
:name
)
');
$stmt->bindParam(':name', $name, PDO::PARAM_STR, 50);
$stmt->execute();
// Check data has actually been inserted
$entries = $dbh->query('
SELECT
*
FROM
Test
')->fetchAll(PDO::FETCH_ASSOC);
var_dump($entries);
// Query DB for non-existent items
$stmt = $dbh->prepare('
SELECT
*
FROM
Test
WHERE
name != :name
');
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->execute();
// How many rows returned
echo $stmt->rowCount();
// Actual data returned
var_dump($stmt->fetch(PDO::FETCH_ASSOC));
?>
I've managed to work-around the problem with some hackery but it'd be nice to not have to do this:
<?php
echo (
(0 == $stmt->rowCount()) ||
(
(1 == $stmt->rowCount()) &&
(false === (($row = $stmt->fetch(PDO::FETCH_ASSOC)))) &&
('0000' == array_pop($dbh->errorInfo()))
)
) ? 'true' : 'false';
?>
Can anyone help or point out any glaring mistakes that I may have made?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PDOStatement::rowCount() 返回
欢迎来到 PDO,在这里,简单的事情有效,不那么简单的事情会毁掉你的一天。 SQLite 是没有可靠的“我的结果集中有多少行?”的驱动程序之一。功能。来自评论:
从 PDOStatement::fetch()< 返回
false
/a> 是“没有返回”的保证,并且您的检查代码完全正常,尽管有点难以阅读。为了您自己的理智,您可能希望考虑包装或派生 PDO 和 PDOStatement。(免责声明:我是 PDO 粉丝。)
PDOStatement::rowCount() returns
Welcome to PDO, where the easy stuff works and the not-so-easy stuff ruins your day. SQLite is one of the drivers that doesn't have a reliable "how many rows are in my result set?" function. From the comments:
A return of
false
from PDOStatement::fetch() is a guarantee of "nothing came back," and your checking code is entirely sane, if a bit hard to read. You may wish to consider wrapping or deriving from PDO and PDOStatement for your own sanity.(Disclaimer: I am a PDO fanboy.)