2014-01-13 65 views
0

我試圖編寫一個cron作業,該作業將運行一個php腳本,該腳本將從固定列的平面(文本)文件中插入數據。php mysql從固定寬度的文本文件插入表

我的文件 '的data.txt' 看起來就像這樣:

first_column     second_column  third_column 

EG。第一列的寬度爲30個字符+ 1個與下一列分隔的空間,第二個20個字符+ 1個分隔空間,第三個15個(包括空格)。我的桌子'TEST'有3列:第一,第二和第三。

問題是,如何先修整列數據,然後將每行插入表中?

<?php 
// initial database stuff 
$host = 'localhost'; 
$user = 'username'; 
$pass = 'password'; 
$db = 'database'; 
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
mysql_select_db($db) or die ("Unable to select database!"); 
$file = file('/home/user/files/data.txt'); # read file into array 
$count = count($file); 
if($count > 0) # file is not empty 
{ 
    $query = "INSERT into TEST(first,second,third) values"; 
    $i = 1; 
    foreach($file as $row) 
    { 
     $query .= "('TRIM(SUBSTR($row,1,30))','TRIM(SUBSTR($row,32,20))','TRIM(SUBSTR($row,34,49))')"; 
     $query .= $i < $count ? ',':''; 
     $i++; 
    } 
mysql_query($query) or die(mysql_error()); 
} 
echo "File data successfully imported to database!!"; 
?> 
+1

在將它們放入Query之前,可能先將它們修剪成PHP? –

+0

如果我是你,我會用PHP做所有不好的工作,而不是MySQL。 –

回答

0

在我結束了使用PDO的建議結束。 它適用於中間帶有逗號的字符串,並跳過空行。

<?php 
// configuration 
$dbtype = 'sqlite'; 
$dbhost = 'localhost'; 
$dbuser = 'user_for_db'; 
$dbpass = 'pass_for_db'; 
$dbname = 'name_of_db'; 

// database connection 
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf8",$dbuser,$dbpass); //charset optional 

// open data file 
$handle = fopen('/home/user/data/file.txt', 'r'); 
if ($handle) 
    { 
    while (($buffer = fgets($handle, 4096)) !== false) 
     { 
     // new data 
     $first  =trim(substr($buffer,0,8)); 
     $second  =trim(substr($buffer,9,10)); 
     $third  =trim(substr($buffer,20,6)); 
     $fourth  =trim(substr($buffer,27,100)); 
     $fifth  =str_replace(" , ", ", ", trim(substr($buffer,128,113))); 
     $sixth  =trim(substr($buffer,240,30)); 

     // query 
     $sql = "INSERT INTO table(column_1,column_2,column_3,column_4,column_5,column_6) VALUES (:first,:second,:third,:fourth,:fifth,:sixth)"; 
     $q = $conn->prepare($sql); 
     $q->execute(array( ':first' =>$first, 
          ':second' =>$second, 
          ':third' =>$third, 
          ':fourth' =>$fourth, 
          ':fifth' =>$fifth, 
          ':sixth' =>$sixth )); 
     } 

     if (!feof($handle)) 
     { 
     echo "Error: unexpected fgets() fail\n"; 
     } 
    fclose($handle); 
    echo "File data successfully imported to database!"; 
} 
?> 
0

這裏是如何看起來像:

<?php 
// initial database stuff 
$host = 'localhost'; 
$user = 'username'; 
$pass = 'password'; 
$db = 'database'; 
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
mysql_select_db($db) or die ("Unable to select database!"); 
$file = file('/home/user/files/data.txt'); # read file into array 
$count = count($file); 
// Edited to add loop back in... Silly me. 
if($count > 0) # file is not empty 

{ 
    foreach ($file as $row){ 
     $first=trim(substr($row,0,30)); 
     $second=trim(substr($row,31,20)); 
     $third=trim(substr($row,33,49)); 

     $query = "INSERT into TEST(first,second,third) values". 
       "($first,$second,$third)"; 
     mysql_query($query) or die(mysql_error()); 
    } 
} 
echo "File data successfully imported to database!!"; 
?> 

考慮使用PDO和事先準備好的聲明,使它更清潔的(更安全)。

+0

對不起,這段代碼是否會插入文件中的所有行? – Mark

+0

Opps它不會。現在修復。 –

+0

在字符串我試圖插入有一些逗號以及。我注意到,無論列中出現逗號,都會出現錯誤。否則,它的作品。我們可以用逗號插入而沒有錯誤嗎? 並且文件也有空行。我們如何跳過這些?謝謝你的幫助。 – Mark

0

試試這個

<?php 
// initial database stuff 
$host = 'localhost'; 
$user = 'username'; 
$pass = 'password'; 
$db = 'database'; 
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
mysql_select_db($db) or die ("Unable to select database!"); 
$file = file('/home/user/files/data.txt'); # read file into array 
$count = count($file); 
if($count > 0) # file is not empty 
{ 
foreach($file as $key => $row) 
{ 
    if($key == 0){ 
     $col_1 = str_replace(" ", "" , trim(substr($row,1,30)); 
     $col_2 = str_replace(" ", "" , trim(substr($row,32,20)); 
     $col_3 = str_replace(" ", "" , trim(substr($row,34,49)); 
     $query = "INSERT into TEST(col_1,col_2,col_3) values"; 
    } 
    else { 
     $query .= "'TRIM(SUBSTR($row,1,30))','TRIM(SUBSTR($row,32,20))','TRIM(SUBSTR($row,34,49))')"; 
     $query .= $key < $count ? ',':''; 
    } 
} 
mysql_query($query) or die(mysql_error()); 
} 
echo "File data successfully imported to database!!"; 
?> 
相關問題