作者nana0130 (小那)
看板Perl
标题[问题] 如何把$变数写入 mysql?
时间Wed Sep 29 14:15:18 2010
程式中,我有两个经过处理的 字串
我想把这两个字串写入mysql
可是都不行耶~~
我在网路上都找不到类似的范例~
sorry 我会赶快去图书馆借书看 QQ
请各位高手帮帮我~~
use File::Find;
sub process_file {
system $File::Find::name if (m/exe$|bat$/);
}
$mainPath="c:\\Program Files\\Java\\jdk1.6.0_21\\bin\\stanford-parser-2008-10-26\\lexparser_1.bat";
find(\&process_file, $mainPath);
open (open_file, "c:\\Program Files\\Java\\jdk1.6.0_21\\bin\\stanford-parser-2008-10-26\\output_1.txt")or "open file error";
while (<open_file>)
{
chomp;
#print STDOUT "$_\n";
$item=$_ ;
print $item; //我要的第一个字串
}
open (open_file, "c:\\Program Files\\Java\\jdk1.6.0_21\\bin\\stanford-parser-2008-10-26\\output_2.txt")or "open file error";
while (<open_file>)
{
chomp;
#print "$_\n";
$ans = $_;
print $ans; //我要的第二个字串
}
use DBI;
use strict;
my $dbh;
if ($dbh=DBI->connect("DBI:mysql:gram_sys","root","XXXXXXXXX") )
{ print "Connect Database:test OK!\n"; }
else
{ print "can't connect database:test!"; exit(0); }
my $sth=$dbh->prepare("INSERT INTO item_set VALUES ($item ,$ans) ");
^^^^^^^^^^^^^^^^^^^^^^^
改成 VALUES ('abcd','efgh' );
就可以新增一笔资料到mysql
我改成$item 就不行
用 '$item' "$item" 也不行
if ($sth->execute){
print "INSERT DATA OK!\n"; }
else{ print "INSERT DATA FAIL!: $dbh->errstr\n"; }
$sth->finish;
$dbh->disconnect;
exit(0);
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.116.39.115