我正在尝试使用perl脚本将数据插入我的数据库,但是我的代码有问题。
i先前创建了一个带有八列的表annotations
,第一个是自动增量主键。我还需要在每一行上填充1
和DB
的最后两个列。
在提示中启动程序时我遇到的错误是:
dbd :: mysql :: st execute失败:不正确的整数值:'null'for column'Id_anntion'在c: users Jean Baptiste Jean Baptiste cours basedonnúes basedonnúes dossier dossier dossier donnúesdonnúes progres第25行,第1行。
ERREUR插入:不正确的整数值:c: Users Jean Baptiste Cours cours basedonnúesdonnúes dossier dossier programs remplissage_table_projet.pl第25行,第1行,第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"
部分有关,但我不能说什么。我认为这不是唯一的问题。
您正在尝试将 string "NULL"
插入 integer column column
如果要覆盖自动启动行为,请在此处使用诸如100的数字
如果您希望MySQL生成数字,则使用零0
或,如果列为NOT NULL
,则可以使用undef
使用perl中的 undef
在mySQL表中插入默认值。因此,在这里您需要修改您的行:
$ins->execute(undef, $tab[0], $tab[1], $tab[2], $tab[3], $tab[4], "1", "DB")
or die "Erreur insertion : " . $ins->errstr();
在这种情况下,它将自动插入列。