如何将数据从文本文件导入到PGSQL数据库表中?

时间:2018-08-22 23:38:27

标签: php postgresql text

我正在尝试将数据从文本文件导入到pgsql数据库表中。没有一个被导入。

下面是我的代码:

<?php
$db = pg_connect("host=localhost port=5432 dbname=my_dataku user=postgres password=S3cr3t179");// Check connection

$handle = fopen($_FILES["uploaded"]["tmp_name"], "r");
if ($handle)
{
while (($line = fgets($handle)) !== false) 
    {

     $lineArr = explode("\t", "$line");
     var_dump($lineArr);
     // instead assigning one by onb use php list -> http://php.net/manual/en/function.list.php            
     list($nmr_fingerprint, $tgl_presensine, $pegawai_idne, $validasi_a, $validasi_b, $stat) = $lineArr;

     // and then insert data
     $query_insert = "INSERT INTO employee (nmr_fingerprint,tgl_presensine,pegawai_idne,validasi_a,validasi_b,stat) VALUES ('$nmr_fingerprint','$tgl_presensine','$pegawai_idne','$validasi_a','$validasi_b','$stat')";
    }
        $insert = pg_query($query_insert);
        $content.= "<div class='success' id='message'>Berhasil di import</div>";
}
fclose($handle);
$content.= <<<EOF
<h4>Import Data</h4>
<form enctype="multipart/form-data" action="" method="post">
    File:<input name="uploaded" type="file" maxlength="20" /><input type="submit" name="upfile" value="Upload File">
</form>
EOF;
echo $content;

我的桌子: My Postgresql Table

我的TXT文件:

My TXT file

我的代码的结果: The result of my code

1 个答案:

答案 0 :(得分:0)

打开日志记录,这样您就可以查看正在显示什么SQL postgresql。在../data/postgresql.conf中,更改以下设置,然后重新启动postgresql。注意-删除#

#log_min_duration_statement = -1    # -1 is disabled, 0 logs all statements
log_min_duration_statement = 0