为什么 PDO 的 Oracle 驱动程序不实现 lastInsertId()?

发布于 2025-01-06 18:43:48 字数 267 浏览 3 评论 0原文

我在 PDO 中收到此错误:

错误:消息:PDO::lastInsertId() [pdo.lastinsertid]: SQLSTATE[IM001]:驱动程序不支持此功能:驱动程序支持 不支持lastInsertId()

当尝试从 Oracle 数据库获取最后插入的 ID 时, 。我将序列字符串添加到最后一个插入 id 函数,但仍然不起作用。对于 Oracle 上的 PDO 错误,Google 没有透露太多信息。

I get this error in PDO:

error: Message: PDO::lastInsertId() [pdo.lastinsertid]:
SQLSTATE[IM001]: Driver does not support this function: driver does
not support lastInsertId()

when trying to get last inserted id from an oracle database. I added the sequence string to the last insert id function but still not working. Google doesn't say much regarding this error on Oracle with PDO.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗地喜欢 2025-01-13 18:43:48

Oracle 没有自动增量列,因此不像 MySQL 那样支持 lastInsertId。您必须使用 Oracle 序列“手动”实现等效的操作。

为每个需要它的表创建一个 oracle 序列,并在需要执行插入时使用 NEXTVAL 检索它,然后在表中插入时使用该值。

$sh = $conn->prepare('SELECT uid_seq.NEXTVAL AS nextInsertID FROM DUAL');
$sh->execute();
$nextInsertId = $sh->fetchColumn(0);

$sh = $conn->prepare("INSERT INTO table (id, data) VALUES(?, 255)");
$sh->execute(array($nextInsertId));

Oracle doesn't have autoincrement columns, so lastInsertId isn't supported in the same way as for MySQL. You have to implement the equivalent "by hand" using Oracle sequences.

Create an oracle sequence for every table that requires it, and use NEXTVAL to retrieve it whenever you need to do an insert, then use that value when inserting in the table.

$sh = $conn->prepare('SELECT uid_seq.NEXTVAL AS nextInsertID FROM DUAL');
$sh->execute();
$nextInsertId = $sh->fetchColumn(0);

$sh = $conn->prepare("INSERT INTO table (id, data) VALUES(?, 255)");
$sh->execute(array($nextInsertId));
爱冒险 2025-01-13 18:43:48

在 Oracle 中,您应该为需要自动增量列的表创建一个 Oracle 序列。

如果你想插入,那么你可以同时完成,而不是查询 nextIncrementId 并插入,如下所示:

$sh = $conn->prepare("INSERT INTO table (id, data) VALUES(SEQUENCE_NAME.NEXTVAL, ?)");
$sh->execute(array($valueToBeInsertedInDataColumn));

如果你只想知道最后一个插入 id,那么不要使用nextval 因为每当您调用它时,它都会将值增加到下一个值。为此,您应该使用 CURRVAL。下面是一个示例:

$sh = $conn->prepare("SELECT SEQUENCE_NAME.CURRVAL AS lastInsertId FROM DUAL");
$lastInserId = $sh->execute();

调试:print_r($lastInserId);

In Oracle, you should have created an oracle sequence for tables which requires an auto increment column.

If you want to insert, then you can do it in the same time rather than query nextIncrementId and insert as below:

$sh = $conn->prepare("INSERT INTO table (id, data) VALUES(SEQUENCE_NAME.NEXTVAL, ?)");
$sh->execute(array($valueToBeInsertedInDataColumn));

If you just want to know the last insert id, then don't use nextval because whenever you call it, it increment the value to the next one. You should use CURRVAL for that purpose. Below is an example:

$sh = $conn->prepare("SELECT SEQUENCE_NAME.CURRVAL AS lastInsertId FROM DUAL");
$lastInserId = $sh->execute();

Debug: print_r($lastInserId);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文