mysql - Perl + dbd/mysql 插入循环数据 + exp reg

标签 mysql perl

嗯,这是我的问题。

我需要在三个 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 中的值发生变化。

('$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

希望您能理解,问候

最佳答案

所以这个:

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);

关于mysql - Perl + dbd/mysql 插入循环数据 + exp reg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12060403/

相关文章:

android - 如何从 Android 应用程序通过 PHP 连接 MySQL 服务器?

mysql - 使用组合键中的一列作为外键

php - session 、位置命令在 000webhost 中不起作用

arrays - 在 perl 中,为什么 push 导致由 qr 创建的正则表达式在未放入双引号时被更改?

perl - Perl如何获取数组引用的最后一个元素的索引?

perl - 在 Perl 中将 UTF8 字符串转换为 ASCII

php - 使用 MySQL 为另一列中的每个唯一值从一列中查找最大值

mysql - Liquibase 部分提交变更集

ios - 如何使用 Perl 开发 iPhone 应用程序?

perl - 在Moose中,如何在设置属性后随时对其进行修改?