【问题标题】:Perl + dbd/mysql insert with loop data + exp regPerl + dbd/mysql 插入循环数据 + exp reg
【发布时间】:2012-08-22 03:54:59
【问题描述】:

嗯,这是我的问题。

我需要在三个tabblas中插入数据,nid、name、dir这些是使用正则表达式从文件中过滤出来的,有上千个,用三分之三的循环来做。

文件 /home/lola/locatebookmarks 中的内容

"date_added": "12988591842733282",
"id": "1706",
"name": "Xenode Systems Blog: \u00BFQu\u00E9 Hacer despu\u00E9s de instalar Fedora 15?",
"type": "url",
"url": "http://otrolink.com.ar/lola.html"
"date_added": "12988591842733884",
"id": "1707",
"name": "Install Google Chrome in Fedora 16 / 15 / 14 using YUM | HowOpenSource",
"type": "url",
"url": "http://www.howopenlola.com/2011/11/"
"date_added": "12988591842734487",
"id": "1708",
"name": "Linuxant - Linux drivers for Conexant chipsets - ALSA driver with improved support for Conexant chipsets",
"type": "url",
"url": "http://urlllola.com/alsa-driver/"

perl 中的这段代码。

use DBI;
use DBD::mysql;

$host = "localhost";
$database = "bookmarks";
$tablename = "test";
$user = "lola";
$pwd = "pass";
$connect = DBI->connect("DBI:mysql:$database:$host", $user, $pwd);

open(FILE, '/home/lola/locatebookmarks');
my @array;
my $var;
while ($i = <FILE>) {
    if ($i =~ /(id|name|http)/) {
        if ($i =~ s/("|:|,|name|id|url)//g) {
            ($key) = $i;
            push(@array, $var);
        }
    }
}
close(FILE);

$n=@array;
$n=($n/=3);
$count = 0;

for ($x = 0; $x < $n; $x++)  {
    while ($count <= 3) {
        $nid = pop(@array);
        $nombre = pop(@array);
        $dir = pop(@array);

        $query_insert = "INSERT INTO $tablename (nid, nombre, dir) 
        VALUES ('$nid', '$nombre', '$dir')";
        $query = $connect->prepare($query_insert);
        $query->execute();

    $count++;
    }
}

在mysql中保存这个,但是那个不好,因为保存“,”!!

,                 Linuxant - Linux drivers for Conexant chipsets - ALSA driver with improved support for Conexant chipsets
,                 1708

,                 Install Google Chrome in Fedora 16 / 15 / 14 using YUM | HowOpenSource
,                 1707

,                 Xenode Systems Blog u00BFQuu00E9 Hacer despuu00E9s de instalar Fedora 15?
,                 1706

, ,  

如果代码 perl 中的 VALUES 发生变化。

('$nid', '$nombre', '$dir')"; > VALUES ('$nid' '$nombre' '$dir')";

它的错误,在控制台中。

DBD::mysql::st execute failed: Column count doesn't match value count at row 1 at exp-b.pl line 38.
DBD::mysql::st execute failed: Column count doesn't match value count at row 1 at exp-b.pl line 38.
DBD::mysql::st execute failed: Column count doesn't match value count at row 1 at exp-b.pl line 38.
DBD::mysql::st execute failed: Column count doesn't match value count at row 1 at exp-b.pl line 38.

我需要在 mysql 中。

        nid         nombre      dir

       1708         Linuxant - Linux drivers for Conexant chipsets - ALSA driver with improved support for Conexant chipsets    http://urlllola.com/alsa-driver/
       1707      Install Google Chrome in Fedora 16 / 15 / 14 using YUM | HowOpenSource     http://www.howopenlola.com/2011/11/
       1706         Xenode Systems Blog: \u00BFQu\u00E9 Hacer despu\u00E9s de instalar Fedora 15?       http://otrolink.com.ar/lola.html

希望你能理解,问候

【问题讨论】:

  • 您应该使用placeholders 而不是直接在查询中使用变量。如果您的变量之一包含撇号怎么办?例如。 'Joe's big problem'.
  • 打印 $query_insert 以查看正在执行的确切 SQL。另外,请按照 TLP 对占位符的建议进行操作。
  • 请使用strictwarnings,注意任何错误,然后请发布您实际运行的完整代码。 (正如其他人指出的那样,您从未设置过$var 等。您的第二组错误来自将问号插入到 SQL 中,但是您的“perl 代码中的值更改”在语法上无效,等等。 )
  • 是的,改变 (nid, nombre, dir) VALUES (?, ?, ?) 但是,一切仍然是平等的,但使用严格和警告? use::strict y use::warnings?

标签: mysql perl


【解决方案1】:

所以这个:

open(FILE, '/home/lola/locatebookmarks');
my @array;
my $var;
while ($i = <FILE>) {
    if ($i =~ /(id|name|http)/) {
        if ($i =~ s/("|:|,|name|id|url)//g) {
            ($key) = $i;
            push(@array, $var);
        }
    }
} 
close(FILE);

永远不要给 $var 设置任何值,所以当你把它推到 @array 上时,你什么也没有推。因此,您可能想将 $i 推到 @array 上而忘记 $key。

这个:

$n=($n/=3);

应该是:

$n /= 3

我第二个 @TLP 关于在 SQL 中使用占位符而不是变量插值。

$query_insert = "INSERT INTO $tablename (nid, nombre, dir) 
VALUES ('$nid', '$nombre', '$dir')";
$query = $connect->prepare($query_insert);
$query->execute();

变成:

$query_insert = "INSERT INTO $tablename (nid, nombre, dir) VALUES (?, ?, ?)";
$query = $connect->prepare($query_insert);
$query->execute($nid, $nombre, $dir);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 2012-07-10
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2011-11-08
    相关资源
    最近更新 更多