我试图用Perl脚本将数据插入到数据库中,但是我的代码有问题。
我以前创建了一个包含八列的表annotations,第一列是一个自动递增的主键。我还需要在每一行上用1和DB填充最后两列。
在提示符中启动程序时会遇到以下错误:
DBD::mysql::st执行失败:不正确的整数值:'id_anntion‘列’id_anntion‘的'NULL’在C:\Users\Jean Baptiste\cours\Base id_anntion第25行,第1行。 Erreur插入:不正确的整数值:C行第1行的'id_anntion‘列’id_anntion‘为'NULL’:\Users\Jean Baptiste\cours\Base id_anntion,第25行,第1行。
这是密码:
use DBI;
use strict;
use utf8;
use warnings;
my ($ligne, @tab);
# Connexion à la base de données
my $dbh = DBI->connect( "DBI:mysql:database=projet;host=localhost", "root", "" )
or die "Erreur de connexion : " . DBI->errstr();
# Gestion de l'encodage UTF-8
$dbh->{'mysql_enable_utf8'} = 1;
$dbh->do('set names utf8');
# Préparation d'une requête pour l'insertion de valeurs dans la BDD
my $ins = $dbh->prepare("INSERT INTO annotations VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
or die "Probleme preparation : " . $dbh->errstr();
open( FILEIN, '<:encoding(utf8)', 'alsace_DB.csv' );
while ( $ligne = <FILEIN> ) {
chomp($ligne);
@tab = split( /;/, $ligne );
$ins->execute("NULL", $tab[0], $tab[1], $tab[2], $tab[3], $tab[4], "1", "DB")
or die "Erreur insertion : " . $ins->errstr();
}
close(FILEIN);
# Déconnexion de la base de données
$dbh->disconnect();我知道这和"NULL"有关,但我说不出是什么。我认为这不是唯一的问题。
发布于 2017-04-30 12:16:33
您正在尝试将字符串"NULL"插入整数列中。
如果您想重写自动增量行为,那么在这里使用一个数字,比如100。
如果希望MySQL生成数字,则使用零0,如果列为NOT NULL,则可以使用undef。
发布于 2017-05-01 17:49:56
在perl中使用undef在MySQL表中插入默认值。因此,这里您需要将行修改为:
$ins->execute(undef, $tab[0], $tab[1], $tab[2], $tab[3], $tab[4], "1", "DB") or die "Erreur insertion : " . $ins->errstr();
在这种情况下,它将自动增加列.
https://stackoverflow.com/questions/43704627
复制相似问题