Zastanawiam się, czy ktoś może wskazać mi w kierunku niektórych wskazówek/skryptu, który pomoże mi utworzyć mysql z oryginalnego pliku dbf, używając PHP.Skrypt PHP przekonwertować pliki .DBF do .MYSQL
dzięki.
Zastanawiam się, czy ktoś może wskazać mi w kierunku niektórych wskazówek/skryptu, który pomoże mi utworzyć mysql z oryginalnego pliku dbf, używając PHP.Skrypt PHP przekonwertować pliki .DBF do .MYSQL
dzięki.
Możesz wypróbować pakiet kompozytora hisamu/php-xbase (https://github.com/hisamu/php-xbase), aby przeczytać plik dbf i wstawić go do swojej bazy danych. Miał ten sam problem i było to najbardziej odpowiednie rozwiązanie.
Można spróbować kod miech
<?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();
}
}
}
?>
Po prostu bądźcie czujni, że 'php_dbase.dll' musi być obecny w folderze' php/ext' i musi być odkomentowany/stworzony w 'php.ini'. (To dlatego, że nie jest domyślny dla php 5.4+ lub ludzi z xampp). –
Wow. To uratowało mój czas. !! –
aktualizacja: w przypadku, gdyby telefon nie skompilować PHP z biblioteką dBase można uzyskać następujący błąd:
Call to undefined function dbase_open()
w takim przypadku (CentOS)
yum install php-dbase
Oto zaktualizowany kod dla $ 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) {
\t switch($col['type']){
\t \t case 'character':
\t \t \t $line[]= "`$col[name]` VARCHAR($col[length])";
\t \t \t break;
\t
\t \t case 'number':
\t \t \t $line[]= "`$col[name]` FLOAT";
\t \t \t break;
\t \t case 'boolean':
\t \t \t $line[]= "`$col[name]` BOOL";
\t \t \t break;
\t \t case 'date':
\t \t \t $line[]= "`$col[name]` DATE";
\t \t \t break;
\t \t case 'memo':
\t \t \t $line[]= "`$col[name]` TEXT";
\t \t \t break;
\t }
}
$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){
\t //global $conn;
\t global $mysqli;
\t if (!$dbf = dbase_open ($dbf_file, 0)){ die("Could not open $dbf_file for import."); }
\t $num_rec = dbase_numrecords($dbf);
\t $num_fields = dbase_numfields($dbf);
\t $fields = array();
\t for ($i=1; $i<=$num_rec; $i++){
\t $row = @dbase_get_record_with_names($dbf,$i);
\t $q = "insert into $db.$table values (";
\t foreach ($row as $key => $val){
\t if ($key == 'deleted'){ continue; }
\t $q .= "'" . addslashes(trim($val)) . "',"; // Code modified to trim out whitespaces
\t }
\t if (isset($extra_col_val)){ $q .= "'$extra_col_val',"; }
\t $q = substr($q, 0, -1);
\t $q .= ')';
\t //if the query failed - go ahead and print a bunch of debug info
\t // if (!$result = mysql_query($q, $conn)){
\t if (!$result = $mysqli->query($q)){
\t \t print (mysql_error() . " SQL: $q\n");
\t \t print (substr_count($q, ',') + 1) . " Fields total.";
\t \t $problem_q = explode(',', $q);
\t \t $q1 = "desc $db.$table";
\t \t //$result1 = mysql_query($q1, $conn);
\t \t $result1 = $mysqli->query($q1);
\t \t $columns = array();
\t \t $i = 1;
\t \t while ($row1 = $result1->fetch_assoc()){
\t \t \t $columns[$i] = $row1['Field'];
\t \t \t $i++;
\t \t }
\t \t $i = 1;
\t \t foreach ($problem_q as $pq){
\t \t \t print "$i column: {$columns[$i]} data: $pq\n";
\t \t \t $i++;
\t \t }
\t \t die();
\t }
}
}
$mysqli->close();
?>
Czy Google 'DBF php konwersji do mysql'? – inhan
tak, ale wynik był błędem – dani