简体   繁体   English

使用Perl程序在MySQL的列中插入多行

[英]Inserting multiple rows in a column in MySQL using a Perl program

I want to insert multiple rows of data in a single column using a single query. 我想使用单个查询在单个列中插入多行数据。 This program is for less data. 该程序用于减少数据。 I have another weather monitoring .txt file which had 4000 lines of data. 我有另一个天气监测.txt文件,有4000行数据。 I can insert one data at a time but it becomes tedious for so many data values. 我可以一次插入一个数据,但是对于这么多的数据值来说这很麻烦。

1.     use DBI;
2.     use DBD::mysql;
3.     use warnings;


4.     $connection = ConnectToMySql($database);

5.   # Multiple Data inputs
6.      $myquery = "INSERT INTO data(datatime,battery)
7.           VALUES
8.             (?,?),
9.             ('16.01.2013','6.54'), #data corresponding to date and battery
10.             ('17.01.2013','6.42'),
11.             ('21.01.2013','6.24'),
12.             ('22.01.2013','6.21'),
13.             ('24.01.2013','6.17'),
14.             ('25.01.2013','6.13'),
15.             ('28.01.2013','6.00'),
16.             ('29.01.2013','5.97'),
17.             ('30.01.2013','5.94'),
18.             ('01.02.2013','5.84')";
19.    $statement2 = $connection->prepare($myquery);

20.     $statement2->execute($myquery);

21.    #--- start sub-routine
22.    sub ConnectToMySql {
23.       $database ="xxxx";
24.       $user = "XXXX";
25.       $pass = "XXXX";
26.       $host="XXXX";
27.    my $dbh = DBI->connect("DBI:mysql:$database:$host", $user, $pass);
28.    }

This code is giving me the following errors: 此代码给出了以下错误:

DBD::mysql::st execute failed: You have an error in your SQL syntax; DBD :: mysql :: st执行失败:您的SQL语法出错; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2 at C:/Users/User/workspace/DataBaseEntry/DataEntry.pl line 20. 检查与您的MySQL服务器版本相对应的手册,以便在C:/Users/User/workspace/DataBaseEntry/DataEntry.pl第20行的第2行''附近使用正确的语法。
DBD::mysql::st execute failed: called with 1 bind variables when 2 are needed at C:/Users/User/workspace/DataBaseEntry/DataEntry.pl line 40. DBD :: mysql :: st执行失败:在C:/Users/User/workspace/DataBaseEntry/DataEntry.pl第40行需要2时,使用1个绑定变量调用。

I cannot identify the problem. 我无法确定问题所在。 Is it the placeholder. 是占位符吗? What can i do to improve it? 我该怎么做才能改善它? I am new to these things. 我是这些东西的新手。 so can you keep it simple. 所以你能保持简单吗? THANKS 谢谢

You should be passing the data values which should replace the (?, ?) as parameters to execute . 您应该传递应该替换(?, ?)作为参数的数据值来execute Your code as written only passes a single parameter to execute and that parameter is the SQL text of your query. 您编写的代码只传递一个参数来execute ,该参数是查询的SQL文本。

Try this instead: 试试这个:

$myquery = "INSERT INTO data(datatime,battery) VALUES (?,?)";
my $sth = $connection->prepare($myquery);

$sth->execute('16.01.2013','6.54');
$sth->execute('17.01.2013','6.42');
$sth->execute('21.01.2013','6.24');
$sth->execute('22.01.2013','6.21');
$sth->execute('24.01.2013','6.17');
$sth->execute('25.01.2013','6.13');
$sth->execute('28.01.2013','6.00');
$sth->execute('29.01.2013','5.97');
$sth->execute('30.01.2013','5.94');
$sth->execute('01.02.2013','5.84');
$connection->do(<<'EOT');
  INSERT INTO data (datatime, battery)
  VALUES
    ('17.01.2013', '6.42'),
    ('21.01.2013', '6.24'),
    ('22.01.2013', '6.21'),
    ('24.01.2013', '6.17'),
    ('25.01.2013', '6.13'),
    ('28.01.2013', '6.00'),
    ('29.01.2013', '5.97'),
    ('30.01.2013', '5.94'),
    ('01.02.2013', '5.84')
EOT

I'm not sure what you're trying to do with placeholders here. 我不确定你在这里尝试使用占位符。

Also, you're missing use warnings; use strict; 此外,您缺少use warnings; use strict; use warnings; use strict; , and you shouldn't use global variables everywhere. ,你不应该在任何地方使用全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM