Created
September 17, 2012 11:53
-
-
Save ilyabrin/3736902 to your computer and use it in GitHub Desktop.
DBF to SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class dbf2sql | |
| { | |
| var $db; | |
| var $num_records; | |
| var $num_fields; | |
| var $nome_arq; | |
| var $table; | |
| var $myfile; | |
| // This function open DBF file, set the variables | |
| // returns 1 if open with success otherwise, returns 0 | |
| function opendb($dbffile, $sqlfile, $tablename) | |
| { | |
| if (($dbffile) && ($sqlfile)) | |
| { | |
| $this->db = @dbase_open($dbffile, 0); | |
| if ($this->db) | |
| { | |
| $this->num_records = dbase_numrecords($this->db); | |
| $this->num_fields = dbase_numfields($this->db); | |
| } | |
| else | |
| { | |
| return(0); | |
| } | |
| $this->nome_arq = $sqlfile; | |
| $this->table = $tablename; | |
| return(1); | |
| } | |
| return(0); | |
| } | |
| // This function open the SQL file for write | |
| // returns 1 if open with success otherwise, returns 0 | |
| function opensql() | |
| { | |
| $this->myfile = @fopen($this->nome_arq, "w"); | |
| if (!$this->myfile) | |
| { | |
| return(0); | |
| } | |
| return(1); | |
| } | |
| // This function close both DBf and SQL files | |
| function closeall() | |
| { | |
| fclose($this->myfile); | |
| dbase_close($this->db); | |
| } | |
| // This function get the columns from DBF file and | |
| // whrite it into a SQL file | |
| function GetColumns($array_columns) | |
| { | |
| $total = count($array_columns); | |
| // loop the database | |
| for($index=1; $index <= $this->num_records; $index ++) | |
| { | |
| $record = dbase_get_record($this->db, $index); // get the actual record | |
| // linha_mens is the SQL instruction to write into SQL file | |
| $linha_mens = "insert into $this->table ( "; | |
| // get the column names | |
| for($count=0; $count<$total; $count++) | |
| { | |
| $linha_mens .= $array_columns[$count][1] . ", "; | |
| } | |
| $linha_mens = substr($linha_mens,0, strlen($linha_mens)-2); | |
| $linha_mens .= " ) values ("; | |
| // get the column values | |
| for($count=0; $count<$total; $count++) | |
| { | |
| $linha_mens .= "'" . trim($record[$array_columns[$count][0]]) . "', "; | |
| } | |
| $linha_mens = substr($linha_mens,0, strlen($linha_mens)-2); | |
| $linha_mens .= " ); \n"; | |
| // Ignore deleted fields | |
| if ($record["deleted"] != "1") | |
| { fputs($this->myfile, "$linha_mens"); } | |
| } | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment