为什么这种带有预准备语句的SQLite事务不起作用? [英] Why does this SQLite transaction with prepared statements not work?

查看:71
本文介绍了为什么这种带有预准备语句的SQLite事务不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将SQLite事务和准备好的语句结合起来,以获得成千上万条记录的最佳插入速度.但是,所有插入的行都是空的.

I'm trying to combine an SQLite transaction and prepared statement to get the best insert speed for thousands of records. However, all the inserted lines are empty.

在插入变量之前打印变量表明它们具有正确的数据并且没有错误.

Printing the variables before inserting shows that they have the correct data and there are no errors.

$db->beginTransaction();
$insert_stmt = $db->prepare("INSERT INTO `table` VALUES (:id, :value2, :value3, :value4)");
$insert_stmt->bindValue(":id", $id);
$insert_stmt->bindValue(":value2", $value2);
$insert_stmt->bindValue(":value3", $value3);
$insert_stmt->bindValue(":value4", $value4);

foreach ($records as $record)
{
  $id = $record["id"];
  $value2 = $record["value2"];
  $value3 = $record["value3"];
  $value4 = $record["value4"];
  $insert_stmt->execute();
  print_r($db->errorInfo()); // print errors
}

$db->commit();

  1. 代码有什么问题?
  2. 如何获得更好的输出?例如,在执行之前打印准备好的语句,以查看其是否有问题.

推荐答案

您必须将bindValue调用放入循环中.

You have to put the bindValue calls into your loop.

当您执行bindValue时,值将被复制到语句中.无论您随后执行此操作的变量发生了什么,都没关系.

The values get copied into the statement when you execute bindValue. Whatever happens to the variable you used to do that afterwards does not matter.

这篇关于为什么这种带有预准备语句的SQLite事务不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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