【问题标题】:PHP Script to convert .DBF files to .MYSQL将 .DBF 文件转换为 .MYSQL 的 PHP 脚本
【发布时间】:2012-12-25 13:36:51
【问题描述】:

只是想知道是否有人可以指出一些提示/脚本的方向,该脚本将帮助我使用 PHP 从原始 dbf 文件创建 mysql。

之前谢谢。

【问题讨论】:

  • 你谷歌了吗php convert dbf to mysql
  • 是的,我做了,但结果是一个错误

标签: php mysql sql phpmyadmin dbf


【解决方案1】:

试试下面的代码...

<?php
    $tbl = "cc";
    $db_uname = 'root';
    $db_passwd = '';
    $db = 'aa';
    $conn = mysql_pconnect('localhost',$db_uname, $db_passwd);

    // Path to dbase file
    $db_path = "dbffile/bbsres12.dbf";

    // Open dbase file
    $dbh = dbase_open($db_path, 0) or die("Error! Could not open dbase database file '$db_path'.");

    // Get column information
    $column_info = dbase_get_header_info($dbh);

    // Display information
    // print_r($column_info);

    $line = array();

    foreach($column_info as $col) {
        switch($col['type']) {
            case 'character':
                $line[]= "`$col[name]` VARCHAR( $col[length] )";
                break;
            case 'number':
                $line[]= "`$col[name]` FLOAT";
                break;
            case 'boolean':
                $line[]= "`$col[name]` BOOL";
                break;
            case 'date':
                $line[]= "`$col[name]` DATE";
                break;
            case 'memo':
                $line[]= "`$col[name]` TEXT";
                break;
        }
    }

    $str = implode(",",$line);
    $sql = "CREATE TABLE `$tbl` ( $str );";

    mysql_select_db($db, $conn);

    mysql_query($sql, $conn);
    set_time_limit(0); // I added unlimited time limit here, because the records I imported were in the hundreds of thousands.

    // This is part 2 of the code

    import_dbf($db, $tbl, $db_path);

    function import_dbf($db, $table, $dbf_file) {
        global $conn;

        if (!$dbf = dbase_open ($dbf_file, 0)) {
            die("Could not open $dbf_file for import.");
        }

        $num_rec = dbase_numrecords($dbf);
        $num_fields = dbase_numfields($dbf);
        $fields = array();

        for ($i=1; $i<=$num_rec; $i++) {
            $row = @dbase_get_record_with_names($dbf,$i);
            $q = "insert into $db.$table values (";

            foreach ($row as $key => $val) {
                if ($key == 'deleted') {
                    continue;
                }
                $q .= "'" . addslashes(trim($val)) . "',"; // Code modified to trim out whitespaces
            }

            if (isset($extra_col_val)) {
                $q .= "'$extra_col_val',";
            }

            $q = substr($q, 0, -1);
            $q .= ')';

            //if the query failed - go ahead and print a bunch of debug info
            if (!$result = mysql_query($q, $conn)) {
                print (mysql_error() . " SQL: $q\n");
                print (substr_count($q, ',') + 1) . " Fields total.";
                $problem_q = explode(',', $q);
                $q1 = "desc $db.$table";
                $result1 = mysql_query($q1, $conn);
                $columns = array();
                $i = 1;
                
                while ($row1 = mysql_fetch_assoc($result1)) {
                    $columns[$i] = $row1['Field'];
                    $i++;
                }

                $i = 1;

                foreach ($problem_q as $pq) {
                    print "$i column: {$columns[$i]} data: $pq\n";
                    $i++;
                }

                die();
            }
        }
    }

?>

【讨论】:

  • 请注意php_dbase.dll 必须存在于php/ext 文件夹中,并且必须在php.ini 中取消注释/创建。 (那是因为它不是 php 5.4+ 或 xampp 的默认设置)。
  • 哇。这节省了我的时间。!!
【解决方案2】:

您可以尝试 composer package hisamu/php-xbase (https://github.com/hisamu/php-xbase) 来读取 dbf 文件并插入到您的数据库中。有同样的问题,这是最合适的解决方案。

【讨论】:

    【解决方案3】:

    更新: 如果您没有使用 dbase 库编译 PHP,您可能会收到以下错误:

    Call to undefined function dbase_open()
    

    在这种情况下(centos)

    yum install php-dbase
    

    这是 $mysqli 的更新代码

    <?php
    
    $mysqli = new mysqli("localhost", "DBusername", "DBpassword", "tableName");
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }
    
    
    $tbl = "yourTableName";
    $db = 'YourDBName';
    
    // Path to dbase file
    $db_path = "/path/dbaseFileName.dbf";
    
    // Open dbase file
    $dbh = dbase_open($db_path, 0)
    or die("Error! Could not open dbase database file '$db_path'.");
    
    // Get column information
    $column_info = dbase_get_header_info($dbh);
    
    $line = array();
    
    foreach($column_info as $col) {
    	switch($col['type']){
    
    		case 'character':
    			$line[]= "`$col[name]` VARCHAR( $col[length] )";
    			break;
    	
    		case 'number':
    			$line[]= "`$col[name]` FLOAT";
    			break;
    
    		case 'boolean':
    			$line[]= "`$col[name]` BOOL";
    			break;
    
    		case 'date':
    			$line[]= "`$col[name]` DATE";
    			break;
    
    		case 'memo':
    			$line[]= "`$col[name]` TEXT";
    			break;
    	}
    }
    
    $str = implode(",",$line);
    $sql = "CREATE TABLE `$tbl` ( $str );";
    
    //mysql_select_db($db,$conn);
    
    //mysql_query($sql,$conn);
    $mysqli->query($sql);
    set_time_limit(0); // I added unlimited time limit here, because the records I imported were in the hundreds of thousands.
    
    // This is part 2 of the code
    
    import_dbf($db, $tbl, $db_path, $mysqli);
    
    function import_dbf($db, $table, $dbf_file,$mysqli){
    	//global $conn;
    	global $mysqli;
    	if (!$dbf = dbase_open ($dbf_file, 0)){ die("Could not open $dbf_file for import."); }
    	$num_rec = dbase_numrecords($dbf);
    	$num_fields = dbase_numfields($dbf);
    	$fields = array();
    
    	for ($i=1; $i<=$num_rec; $i++){
    	$row = @dbase_get_record_with_names($dbf,$i);
    	$q = "insert into $db.$table values (";
    	foreach ($row as $key => $val){
    	if ($key == 'deleted'){ continue; }
    	$q .= "'" . addslashes(trim($val)) . "',"; // Code modified to trim out whitespaces
    	}
    
    	if (isset($extra_col_val)){ $q .= "'$extra_col_val',"; }
    	$q = substr($q, 0, -1);
    	$q .= ')';
    	//if the query failed - go ahead and print a bunch of debug info
    	// if (!$result = mysql_query($q, $conn)){
    	if (!$result = $mysqli->query($q)){
    		print (mysql_error() . " SQL: $q\n");
    		print (substr_count($q, ',') + 1) . " Fields total.";
    
    		$problem_q = explode(',', $q);
    		$q1 = "desc $db.$table";
    		//$result1 = mysql_query($q1, $conn);
    		$result1 = $mysqli->query($q1);
    		$columns = array();
    
    		$i = 1;
    
    		while ($row1 = $result1->fetch_assoc()){
    			$columns[$i] = $row1['Field'];
    			$i++;
    		}
    
    		$i = 1;
    		foreach ($problem_q as $pq){
    			print "$i column: {$columns[$i]} data: $pq\n";
    			$i++;
    		}
    		die();
    	}
    }
    }
    
    $mysqli->close();
    
    ?>

    【讨论】:

      【解决方案4】:

      <?php
      
      $mysqli = new mysqli("localhost", "DBusername", "DBpassword", "tableName");
      if ($mysqli->connect_errno) {
          echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
      }
      
      
      $tbl = "yourTableName";
      $db = 'YourDBName';
      
      // Path to dbase file
      $db_path = "/path/dbaseFileName.dbf";
      
      // Open dbase file
      $dbh = dbase_open($db_path, 0)
      or die("Error! Could not open dbase database file '$db_path'.");
      
      // Get column information
      $column_info = dbase_get_header_info($dbh);
      
      $line = array();
      
      foreach($column_info as $col) {
      	switch($col['type']){
      
      		case 'character':
      			$line[]= "`$col[name]` VARCHAR( $col[length] )";
      			break;
      	
      		case 'number':
      			$line[]= "`$col[name]` FLOAT";
      			break;
      
      		case 'boolean':
      			$line[]= "`$col[name]` BOOL";
      			break;
      
      		case 'date':
      			$line[]= "`$col[name]` DATE";
      			break;
      
      		case 'memo':
      			$line[]= "`$col[name]` TEXT";
      			break;
      	}
      }
      
      $str = implode(",",$line);
      $sql = "CREATE TABLE `$tbl` ( $str );";
      
      //mysql_select_db($db,$conn);
      
      //mysql_query($sql,$conn);
      $mysqli->query($sql);
      set_time_limit(0); // I added unlimited time limit here, because the records I imported were in the hundreds of thousands.
      
      // This is part 2 of the code
      
      import_dbf($db, $tbl, $db_path, $mysqli);
      
      function import_dbf($db, $table, $dbf_file,$mysqli){
      	//global $conn;
      	global $mysqli;
      	if (!$dbf = dbase_open ($dbf_file, 0)){ die("Could not open $dbf_file for import."); }
      	$num_rec = dbase_numrecords($dbf);
      	$num_fields = dbase_numfields($dbf);
      	$fields = array();
      
      	for ($i=1; $i<=$num_rec; $i++){
      	$row = @dbase_get_record_with_names($dbf,$i);
      	$q = "insert into $db.$table values (";
      	foreach ($row as $key => $val){
      	if ($key == 'deleted'){ continue; }
      	$q .= "'" . addslashes(trim($val)) . "',"; // Code modified to trim out whitespaces
      	}
      
      	if (isset($extra_col_val)){ $q .= "'$extra_col_val',"; }
      	$q = substr($q, 0, -1);
      	$q .= ')';
      	//if the query failed - go ahead and print a bunch of debug info
      	// if (!$result = mysql_query($q, $conn)){
      	if (!$result = $mysqli->query($q)){
      		print (mysql_error() . " SQL: $q\n");
      		print (substr_count($q, ',') + 1) . " Fields total.";
      
      		$problem_q = explode(',', $q);
      		$q1 = "desc $db.$table";
      		//$result1 = mysql_query($q1, $conn);
      		$result1 = $mysqli->query($q1);
      		$columns = array();
      
      		$i = 1;
      
      		while ($row1 = $result1->fetch_assoc()){
      			$columns[$i] = $row1['Field'];
      			$i++;
      		}
      
      		$i = 1;
      		foreach ($problem_q as $pq){
      			print "$i column: {$columns[$i]} data: $pq\n";
      			$i++;
      		}
      		die();
      	}
      }
      }
      
      $mysqli->close();
      
      ?>

      【讨论】:

        猜你喜欢
        • 2012-02-16
        • 2011-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-15
        • 1970-01-01
        • 1970-01-01
        • 2011-05-25
        相关资源
        最近更新 更多