note 82405 added to ref.dbase

From: Date: Wed, 09 Apr 2008 14:40:08 +0000
Subject: note 82405 added to ref.dbase
Groups: php.notes 
Request: Send a blank email to php-notes+get-138065@lists.php.net to get a copy of this message
For all those of you who wants to DBF->SQL, here's half of a hour worth of work. It's pretty simple, outputs a CREATE TABLE and lots of INSERTs. The output is HTML, as I use w$ndows here at work and prefer to just see results in opera. You SHOULD verify the created data, add some CREATE KEYs, NOT NULLs or whatever you like. <?php $file_name = "mfiscal/dados22/APISS110.D00"; $table = "dbf_apiss"; $dbf = @dbase_open($file_name, 0) or die("Error opening $file_name"); $fields = dbase_get_header_info($dbf); /* Just show a table with header information */ echo "<table cellspacing=0><tr><th>Name</th><th>Type</th> <th>Length</th><th>Precision</th><th>Format</th><th>Offset</th></tr>"; foreach($fields as $field) { echo "<tr> <td>{$field['name']}</td> <td>{$field['type']}</td> <td>{$field['length']}</td> <td>{$field['precision']}</td> <td>{$field['format']}</td> <td>{$field['offset']}</td> </tr>"; } echo "</table><br><br>"; /* Here begins the SQL code part */ echo "CREATE TABLE $table (<BR>"; $x = 0; $fields_num = 0; foreach($fields as $field) { if($x++ != 0) echo ",<BR>"; switch($field['type']) { case 'character' : $type = 'CHAR'; $length = $field['length'] > 1 ? "({$field['length']})" : ""; break; case 'number' : $type = 'NUMERIC'; $length = "({$field['length']}" . ($field['precision'] > 0 ? ", {$field['precision']})": ")"); break; // case '' : $type = ''; break; } $fields_num++; echo "&nbsp;&nbsp;&nbsp;{$field['name']} $type$length"; } echo ");<BR><BR>BEGIN;<BR>"; $records = @dbase_numrecords($dbf) or die("Error reading DBF's number of fields"); for($x = 1; $x <= $records; $x++) { $record = dbase_get_record($dbf, $x); echo "INSERT INTO $table VALUES ("; $f = 0; foreach($record as $field) { if($f != 0) echo ", "; if(strcmp(str_repeat(' ', $fields[$f]['length']), $field) != 0) { if($fields[$f]['type'] == 'character') echo "'"; echo $field; if($fields[$f]['type'] == 'character') echo "'"; } else echo "NULL"; if(++$f >= $fields_num) break; } echo ");<BR>"; } echo "COMMIT;"; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 201.62.136.13 ---- Manual Page -- http://www.php.net/manual/en/ref.dbase.php Edit -- https://master.php.net/note/edit/82405 Del: integrated -- https://master.php.net/note/delete/82405/integrated Del: useless -- https://master.php.net/note/delete/82405/useless Del: bad code -- https://master.php.net/note/delete/82405/bad+code Del: spam -- https://master.php.net/note/delete/82405/spam Del: non-english -- https://master.php.net/note/delete/82405/non-english Del: in docs -- https://master.php.net/note/delete/82405/in+docs Del: other reasons-- https://master.php.net/note/delete/82405 Reject -- https://master.php.net/note/reject/82405 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138065) next »