批量插入和准备查询错误

发布于 2024-09-06 04:19:04 字数 1078 浏览 11 评论 0原文

好的,所以我需要使用 MySQL 查询的结果填充 MS Access 数据库表。这一点也不难。我已将程序写入其中,它将模板 .mdb 文件复制到临时名称并通过 odbc 打开它。到目前为止没有问题。

我注意到 Access 不支持批量插入(VALUES (foo, bar), (second, query), (third query))。因此,这意味着我需要每行执行一个查询(可能有数十万行)。初始性能测试显示 Access 的插入速度约为每秒 900 次。对于我们最大的数据集,这可能意味着执行时间为几分钟(这不是世界末日,但显然越快越好)。

因此,我尝试测试准备好的声明。但我不断收到错误(警告:odbc_execute() [function.odbc-execute]:SQL 错误:[Microsoft][ODBC Microsoft Access Driver]COUNT 字段不正确,D:\.. 中的 SQLExecute 中的 SQL 状态 07001 ..php 第 30 行)。

这是我正在使用的代码(第 30 行是 odbc_execute ):

$sql = 'INSERT INTO table 
    ([field0], [field1], [field2], [field3], [field4], [field5]) 
    VALUES (?, ?, ?, ?, ?, ?)';
$stmt = odbc_prepare($conn, $sql);
for ($i = 200001; $i < 300001; $i++) {
    $a = array($i, "Field1 $", "Field2 $i", "Field3 $i", "Field4 $i", $i);
    odbc_execute($stmt, $a);
}

所以我的问题有两个。首先,是否知道为什么我会收到该错误(我已经检查过,数组中的数字与与参数 ? 标记的数量匹配的字段列表相匹配)?其次,我应该为此烦恼还是只使用直接的 INSERT 语句?就像我说的,时间并不重要,但如果可能的话,我希望时间尽可能短(话又说回来,我可能会受到磁盘吞吐量的限制,因为 900 次操作/秒已经很高了).. 。

谢谢

Ok, so I need to populate a MS Access database table with results from a MySQL query. That's not hard at all. I've got the program written to where it copies a template .mdb file to a temp name and opens it via odbc. No problem so far.

I've noticed that Access does not support batch inserting (VALUES (foo, bar), (second, query), (third query)). So that means I need to execute one query per row (there are potentially hundreds of thousands of rows). Initial performance tests show a rate of around 900 inserts/sec into Access. With our largest data sets, that could mean execution times of minutes (Which isn't the end of the world, but obviously the faster the better).

So, I tried testing a prepared statement. But I keep getting an error (Warning: odbc_execute() [function.odbc-execute]: SQL error: [Microsoft][ODBC Microsoft Access Driver]COUNT field incorrect , SQL state 07001 in SQLExecute in D:\....php on line 30).

Here's the code I'm using (Line 30 is odbc_execute):

$sql = 'INSERT INTO table 
    ([field0], [field1], [field2], [field3], [field4], [field5]) 
    VALUES (?, ?, ?, ?, ?, ?)';
$stmt = odbc_prepare($conn, $sql);
for ($i = 200001; $i < 300001; $i++) {
    $a = array($i, "Field1 $", "Field2 $i", "Field3 $i", "Field4 $i", $i);
    odbc_execute($stmt, $a);
}

So my question is two fold. First, is there any idea on why I'm getting that error (I've checked, and the number in the array matches the field list which matches the number of parameter ? markers)? And second, should I even bother with this or just use the straight INSERT statements? Like I said, time isn't critical, but if it's possible, I'd like to get that time as low as possible (Then again, I may be limited by disk throughput, since 900 operations/sec is high already)...

Thanks

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

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

发布评论

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

评论(2

稚然 2024-09-13 04:19:05

PHP 是否为您提供了查看参数替换后正在执行的 INSERT 语句的方法?我认为您可能没有在 VALUES 列表中获得有关文本值的引号。如果没有引号,Jet 数据库引擎会将“Field1 200001”解释为两个值而不是一个。

另外,我不知道 PHP,但是数组的第二个成员应该是“Field1 $i”而不是“Field1 $”吗?

你能从 PHP 执行这个语句吗?它有效吗?

INSERT INTO table 
    ([field0], [field1], [field2], [field3], [field4], [field5]) 
VALUES (
    200001,
    'Field1 200001',
    'Field2 200001',
    'Field3 200001',
    'Field4 200001',
    200001);

这个怎么样?

$sql = 'INSERT INTO table 
    ([field0], [field1], [field2], [field3], [field4], [field5]) 
    VALUES (?, "?", "?", "?", "?", ?)';

Does PHP give you a way to view the INSERT statement you're executing after parameter replacement? I think you may not be getting quotes around text values in your VALUES list. Without quotes, the Jet database engine will interpret 'Field1 200001' as two values rather than one.

Also, I don't know PHP, but should the second member of your array be "Field1 $i" instead of "Field1 $"?

Can you execute this statement from PHP? And does it work?

INSERT INTO table 
    ([field0], [field1], [field2], [field3], [field4], [field5]) 
VALUES (
    200001,
    'Field1 200001',
    'Field2 200001',
    'Field3 200001',
    'Field4 200001',
    200001);

How about this?

$sql = 'INSERT INTO table 
    ([field0], [field1], [field2], [field3], [field4], [field5]) 
    VALUES (?, "?", "?", "?", "?", ?)';
浊酒尽余欢 2024-09-13 04:19:04

您需要逐行执行此操作吗?为什么不一次插入所有数据呢?

什么是MS Access 和 MySQL 之间同步数据的最佳方式?

Do you need to do this row by row? Why not insert all the data at once?

What is the best way to synchronize data between MS Access and MySQL?

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