phpStudy怎么导入sql文件啊?

phpStudy怎么导入sql文件啊?,第1张

点击MySQL管理器--》点击进入MySQL-Front--》在localhost下选择新建一个数据库--》点击新建数据库后,在对象浏览器窗口,单击右键--》输入--》sql文件命令,进入后选择对应.sql文件进行导入(注意:字符集格式选择)

本篇文章是对使用PHP执行 SQL文件的实现代码进行了详细的分析介绍 需要的朋友参考下  

demo php:

复制代码 代码如下: <?php /** * 读取 sql 文件并写入数据库 * @version demo php */ class DBManager {     var $dbHost =     var $dbUser =     var $dbPassword =     var $dbSchema =     function __construct($host $user $password $schema)     {         $this >dbHost = $host        $this >dbUser = $user        $this >dbPassword = $password        $this >dbSchema = $schema    }     function createFromFile($sqlPath $delimiter = (/n)|((/r/n))|(/r) $prefix = $menter = array( # ))     {         //判断文件是否存在         if(!file_exists($sqlPath))             return false        $handle = fopen($sqlPath rb )           $sqlStr = fread($handle filesize($sqlPath))        //通过sql语法的语句分割符进行分割         $segment = explode("" trim($sqlStr))        //var_dump($segment)        //去掉注释和多余的空行         foreach($segment as &$statement)         {             $sentence = explode("/n" $statement)            $newStatement = array()            foreach($sentence as $subSentence)             {                 if( != trim($subSentence))                 {                     //判断是会否是注释                     $isComment = false                    foreach($menter as $er)                     {                         if(eregi("^(" $er ")" trim($subSentence)))                         {                             $isComment = true                            break                        }                     }                     //如果不是注释 则认为是sql语句                     if(!$isComment)                         $newStatement[] = $subSentence                                   }             }             $statement = $newStatement        }         //对表名加前缀         if( != $prefix)         {                    //只有表名在第一行出现时才有效 例如 CREATE TABLE talbeName             $regxTable = "^[/`/ /"]{ }[/_a zA Z]+[/_a zA Z ]*[/`/ /"]{ }$"//处理表名的正则表达式             $regxLeftWall = "^[/`/ /"]{ }"            $sqlFlagTree = array(                     "CREATE" =>array(                             "TABLE" =>array(                                     "$regxTable" =>                                )                         )                     "INSERT" =>array(                             "INTO" =>array(                                 "$regxTable" =>                            )                         )                     )            foreach($segment as &$statement)             {                 $tokens = split(" " $statement[ ])                $tableName = array()                $this >findTableName($sqlFlagTree $tokens $tableName)                if(empty($tableName[ leftWall ]))                 {                     $newTableName = $prefix $tableName[ name ]                }                 else{                     $newTableName = $tableName[ leftWall ] $prefix substr($tableName[ name ] )                }                 $statement[ ] = str_replace($tableName[ name ] $newTableName $statement[ ])            }         }               //组合sql语句         foreach($segment as &$statement)         {             $newStmt =             foreach($statement as $sentence)             {                 $newStmt = $newStmt trim($sentence) "/n"            }             $statement = $newStmt        }         //用于测试                //var_dump($segment)        //writeArrayToFile( data txt $segment)        //         self::saveByQuery($segment)        return true    }     private function saveByQuery($sqlArray)     {         $conn = mysql_connect($this >dbHost $this >dbUser $this >dbPassword)        mysql_select_db($this >dbSchema)        foreach($sqlArray as $sql)         {             mysql_query($sql)        }               mysql_close($conn)    }     private function findTableName($sqlFlagTree $tokens $tokensKey= &$tableName = array())     {         $regxLeftWall = "^[/`/ /"]{ }"        if(count($tokens)<=$tokensKey)             return false               if( == trim($tokens[$tokensKey]))         {             return self::findTableName($sqlFlagTree $tokens $tokensKey+ $tableName)        }         else         {             foreach($sqlFlagTree as $flag =>$v)             {                   if(eregi($flag $tokens[$tokensKey]))                 {                     if( ==$v)                     {                         $tableName[ name ] = $tokens[$tokensKey]                        if(eregi($regxLeftWall $tableName[ name ]))                         {                             $tableName[ leftWall ] = $tableName[ name ]{ }                        }                         return true                    }                     else{                         return self::findTableName($v $tokens $tokensKey+ &$tableName)                    }                 }             }         }         return false    } } function writeArrayToFile($fileName $dataArray $delimiter="/r/n") {     $handle=fopen($fileName "wb")    $text =     foreach($dataArray as $data)     {         $text = $text $data $delimiter    }     fwrite($handle $text)} //测试 $dbM = new DBManager( localhost w f test )$dbM >createFromFile( data sql null fff_ )?>lishixinzhi/Article/program/PHP/201311/21281

<?php

$file_name = "d:test.sql"

$dbhost = "localhost"

$dbuser = "root"

$dbpass = "123456"

$dbname = "test"

set_time_limit(0)

$fp = @fopen($file_name,"r") or die("sql文件打不开")//打开文件

$pdo = new PDO("mysql:host=localhostdbname=test","root","123456")//连接数据库

$pdo->query('set names utf8')//设置编码

echo "正在执行导入 *** 作"

while($SQL = GetNextSQL()){

if(!$pdo->query($SQL)){

echo "执行出错"

echo "SQL语句为".$SQL

}

}

echo "导入完成"

fclose($fp) or die("can't close file")//关闭文件

mysql_close()

//从文件中逐条取sql

function GetNextSQL(){

global $fp

$sql=""

while($line = @fgets($fp,40960)){

$line = trim($line)

$line = str_replace("////", "//", $line)

$line = str_replace("/","'",$line)

$line = str_replace("//r//n","chr(13).chr(10)",$line)

$line = stripcslashes($line)

if(strlen($line)>1){

if($line[0]=='-' &&$line[1]=="-"){

continue

}

}

$sql .= $line.chr(13).chr(10)

if(strlen($line)>0){

if($line[strlen($line)-1]==""){

break

}

}

}

return $sql

}

亲测有效。。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11736204.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存