|
以php cli脚本程序来实现文本数据插入数据库.该文本是以tab分割的.
代码如下:
[PHP]#!/usr/bin/php
<?php
if (!$argv[1]){
echo "Usageargv[0] filename!";
exit;
}
$connect = pg_connect("host=localhost dbname=mydb user=postgres password=123") or die("数据库连接错误");
$infile=fopen($argv[1],"r") or die ("Cannot open file $argv[1]\n");
while (!feof ($infile)){
$line = fgets($infile,8192);
$table= "basic";
$array = explode("\t",$line);
$sql = "insert into $table value (";
while (list($name,$value) = each($array)){
$sql .="$value,";
}
$sql .=");"; echo "$sql\n";
$res =pg_query($connect,$sql);
if($res){
echo "insert data succesfully\n";
}
else{
echo "Wrong inputs\n";
}
} [/PHP]
执行该脚本的时候,echo $sql的结果:
insert into basic value ("R1017","XJWE0298",,,"印度尼西亚","印度尼西亚","印度尼 西亚",,,,,"国外种质" ,);
该sql语句应该没有错误.
但执行到pg_query的时候,提示说:
Warning: pg_query(): Query failed: 错误: parser: parse error at or near "value" at character 19 . in /home/ch/db/connect.php on line 64 错误: parser: parse error at or near "value" at character 19 Wrong inputs
本来打算用pg_insert函数,但涉及到一个数组的结构问题,即必须用 field=>value 结构,很麻烦. |
|