0

现在我有这样的方法:

 function exportFromTransbase($table_name) {
 $odbc_query = "SELECT * FROM " . $table_name;

 $data = odbc_exec($this->odbc_id, $odbc_query);
 odbc_longreadlen($data, 10485760);

 while($row = odbc_fetch_array($data))
 {

 foreach($row as $key => $value) {
 $keys[] = "`" . $key . "`";
 $values[] = "'" . mysql_real_escape_string($value) . "'";
 }


 $mysql_query = "INSERT INTO `" . strtolower(substr($table_name, 4)) . "` (" . implode(",", $keys) . ") VALUES (" . implode(",", $values) . ")";

 mysql_query($mysql_query);
 set_time_limit(3600);
 unset($keys);
 unset($values);
 unset($row);
 }
    if ($mysql_query){
        print "Ýêñïîðò äàííûõ èç òàáëèöû " . $table_name . " çàâåðøåí!";
        //strtolower(substr($table_name, 4))
    }
 }

但是导入mysql的时候很慢。我决定将其更改为导出到文件 .sql,以便将来我可以通过终端或 phpmyadmin 导入该表。如何更改导出到 sql 文件我的数据?

笔记!我正在从 transbase 转换为 mysql

4

2 回答 2

0

代替...

while($row = odbc_fetch_array($data))
{
   foreach($row as $key => $value) {
      $keys[] = "`" . $key . "`";
      $values[] = "'" . mysql_real_escape_string($value) . "'";
   }


   $mysql_query = "INSERT INTO `" . strtolower(substr($table_name, 4)) . "` (" . implode(",", $keys) . ") VALUES (" . implode(",", $values) . ")";

    mysql_query($mysql_query);
    set_time_limit(3600);  // this should not be here
    unset($keys);          // this is redundant
    unset($values);        // and this
    unset($row);           // and this too
 }

尝试:

$oufile=fopen("export.sql", 'w') || die("error writing file");

while($row = odbc_fetch_array($data))
{
   foreach($row as $key => $value) {
      $keys[] = "`" . $key . "`";
      $values[] = "'" . mysql_real_escape_string($value) . "'";
   }


   $mysql_query = "INSERT INTO `" . strtolower(substr($table_name, 4)) . "` (" . implode(",", $keys) . ") VALUES (" . implode(",", $values) . ")";

   fputs($outfile, $mysql_query . ";\n";        
 }

但是,如果您...会更快。

$oufile=fopen("export.sql", 'w') || die("error writing file");
fputs($outfile, "ALTER TABLE `" . . strtolower(substr($table_name, 4)) . "` DISABLE KEYS;\n";

while($row = odbc_fetch_array($data))
{
   foreach($row as $key => $value) {
      $keys[] = "`" . $key . "`";
      $values[] = "'" . mysql_real_escape_string($value) . "'";
   }

   $head="INSERT DELAYED INTO `" . strtolower(substr($table_name, 4)) . "` (" . implode(",", $keys) . ") VALUES ";
   $row[]="(" . implode(",", $values) . ")";

   if (count($row)>100) {
      flush_ins($outfile, $head, $row);
      $row=array();
   }       
 }
 if (count($row)) flush_ins($outfile, $head, $row);
 fputs($outfile, "ALTER TABLE `" . . strtolower(substr($table_name, 4)) . "` ENABLE KEYS;\n";
 fclose($outfile);
...
function flush($rows, $head, $fh) 
 {
    fputs($fh, $head . implode("\n,", $rows) . ";\n");
 }
于 2012-07-16T14:33:48.327 回答
0

看到这个帖子:

无需访问服务器或 phpMyADMIN 即可导出 SQL 表的简便方法

它使用select into outfile语法。这种语法的文档在这里:http ://dev.mysql.com/doc/refman/5.1/en/select-into.html

你会做这样的事情:

mysql_query('SELECT * INTO OUTFILE "/path/to/my_file/my_file.sql" from '.$table_name)

然后这个 .sql 文件将在您的服务器上。

如果您没有运行select into outfilesynatx 的权限。您可以使用该mysqldump实用程序,如下所示:

<?php
    exec('mysqldump db_name '.$table_name.' > my_file.sql');
?>

这将创建一个带有指定名称的 .sql 文件。

于 2012-07-16T13:45:06.103 回答