亲吻原则 http://en.wikipedia.org/wiki/KISS_principle:只使用 phpMyAdmin 吗?几乎可以肯定它已安装。如果不是,安装它 http://phpmyadmin.net/.
其进口能力非常强大。如果您的数据库太大,请对其进行 gzip。如果它仍然太大,请尝试将其分成几块。我怀疑您是否需要将其作为单个大交易进行转移。你?
在第一条评论的解释之后,好吧,就到这里。这是我非常简单的脚本,可以满足您的要求。但它不考虑分隔符:一个查询 == 一行。
<link rel="stylesheet" href="style/contents.css"/>
<?
function timesanitize($v) {
if ($v > 0)
return round($v, 4);
else
return 0;
}
$startmt = microtime();
include_once 'include/db.php';
$f = fopen("db.sql","r");
echo dbGetEngine() . "<br>";
echo "<ul>";
do {
$l = rtrim(fgets($f));
if (strlen($l) == 0)
continue;
if (substr($l, 0, 1) == '#')
continue;
$l = str_replace(
array("\\n"),
array("\n"),
$l);
if (dbGetEngine() == "pgsql")
$l = str_replace(
array("IF NOT EXISTS", "LONGBLOB"),
array("", "TEXT"),
$l);
try {
echo "<li>".nl2br(htmlspecialchars($l));
$mt = microtime();
$db->query($l);
echo "<ul><li>ok - " . timesanitize(microtime() - $mt) . "</ul>";
} catch (PDOException $e) {
echo "<ul><li>".$e->getMessage() . "</ul>";
}
} while (!feof($f));
fclose($f);
echo 'total: ' . timesanitize(microtime() - $startmt);
?>
它还输出每个查询花费多长时间的小统计信息。它基于 PDO;我相信PDO是在PHP5.1或PHP5.2中引入的。我认为修改它以直接使用应该是微不足道的mysql_*()
函数,如果出于某种原因你更喜欢它。
再说一遍:是的,我知道这很糟糕。但只要它对我有用(tm),也可能对你有用......:-)
要完成代码,这里有include/db.php
和一个样品include/config.php
:
include/db.php
:
<?
include_once 'include/config.php';
try {
$attribs =
array(
PDO::ATTR_PERSISTENT => $config['db']['persistent'],
PDO::ATTR_ERRMODE => $config['db']['errormode']
);
$db = new PDO(
$config['db']['uri'],
$config['db']['user'],
$config['db']['pass'],
$attribs
);
$db->query("SET NAMES 'utf8'");
$db->query("SET CHARACTER SET 'utf8'");
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
function dbGetEngine() {
global $config;
return substr($config['db']['uri'], 0, strpos($config['db']['uri'], ':'));
}
?>
include/config.php
:
<?
//$config['db']['uri'] = 'sqlite:' . realpath('.') . '/site.db'; // PDO's database access URI
$config['db']['uri'] = 'mysql:host=localhost;dbname=sitedb'; // server should be : 195.78.32.7
//$config['db']['uri'] = 'pgsql:host=localhost;dbname=sitedb';
$config['db']['user'] = 'user_goes_here'; // database username
$config['db']['pass'] = 'pass_goes_here'; // database password
$config['db']['persistent'] = false; // should the connection be persistent
$config['db']['errormode'] = PDO::ERRMODE_EXCEPTION; // PDO's error mode
?>
其中包括 SQLite、MySQL 和 PostgreSQL 的示例连接字符串。