2013-06-05 79 views
-2

我試圖用perl DBI將perl生成的數據插入到oracle數據庫表中。這是我第一次嘗試這個,所以我很可能犯了一個簡單的錯誤。將數據從perl插入到mysql

我使用的時刻是什麼:

$dbh = DBI->connect ('dbi:Oracle:geosgen','student','train') 
    || die "Database connection not made: $DBI::errstr"; 

    $sql = "INSERT INTO MYTEST VALUES ($series, $sheet, $maxlat, $minlat, $maxlong, $minlong)"; 

    $create = $dbh->do($sql); 
    print "Content-type:text/html\n\n\n"; 
if($Create){ 
print "Success"; 
} 
else{ 
print "Failure<br/>$DBI::errstr"; 
} 

和輸出我得到的是這樣的:

DBD::Oracle::db do failed: ORA-00917: missing comma (DBD ERROR: error possibly near <*> indicator at char 36 in 'INSERT INTO MYTEST VALUES (Scotland <*>one inch 1st, Shetland Islands (North) (131), -0.6800, -1.4100, 60.9700, 60.6900)') [for Statement "INSERT INTO MYTEST VALUES (Scotland one inch 1st, Shetland Islands (North) (131), -0.6800, -1.4100, 60.9700, 60.6900)"] at bruce1.pl line 69, <INPUT> line 924. Content-type:text/html

這意味着它具有讀取成功的數據,但是失敗了。我希望我犯了一個菜鳥錯誤,有什麼明顯的嗎?

+2

難道你不知道插入變量到查詢中是危險的嗎?請儘可能考慮使用參數化語句:'my $ sth = $ dbh-> prepare('INSERT ...(?,?,...)'; $ sth-> execute($ series,$ sheet,...);'。另外,Perl變量是區分大小寫的:'$ create'和'$ Create'是無關的。 – amon

回答

4

SQL需要字母常數被引用:

INSERT INTO MYTEST VALUES( '蘇格蘭一英寸1', '設得蘭羣島(北)(131)',-0.6800,-1.4100,60.9700,60.6900)

如果您打印$ sql的值,您將看到它們不是。

+0

是的,但是如果您在查詢中使用綁定點,則無需擔心引用字符串。 –