将dbf转换成mysql_用于将.DBF文件转换为.MYSQL的PHP​​脚本

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,-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();

}

}

}

?>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值