![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
由于数据库是直接备份成了一个100多M的.sql文件 phpmyadmin不能直接导入 所以写了一个程序用来 导入大个的数据库备份文件~
建了一个空库 运行下面的程序 成功地创建了表 可是数据却没有导入 晕晕的~
[PHP]<?php
$db_host='localhost';
$db_user='root';
$db_pass='';
$db_name='zzz';
$sql_file='phpwind.sql';
$lines_once=1000;
//----------------------------------------------------------
mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_name);
$fp=fopen($sql_file,'r');
while (!feof($fp)) {
$temp=fgets($fp);
if(strpos($temp,'#')!==0 && strpos($temp,"\r\n")!==0){
if(strpos($temp,'CREATE TABLE')===0){
get_create($temp);
}elseif(strpos($temp,'INSERT INTO')===0){
get_insert($temp);
}
}
}
mysql_close();
echo "SQL INPUT OK!";
//数据库表创建函数
function get_create($head){
global $fp;
$sql=$head;
for($i=0;$i<100;$i++){
$temp=fgets($fp);
$sql.=$temp;
if($temp[strlen($temp)-3]==';'){
break;
}
}
mysql_query($sql);
}
//数据库数据导入函数
function get_insert($head){
global $fp,$lines_once;
$sql=$head;
for($i=0;$i<$lines_once && !feof($fp);$i++){
$temp=fgets($fp);
if(strpos($temp,'#')===0 || strpos($temp,"\r\n")===0){
continue;
}
if(strpos($temp,'CREATE TABLE')===0){
get_create($temp);
break;
}
$sql.=$temp;
}
mysql_query($sql);
}
?>
[/PHP]

