我想将MS Access数据导入MySQL数据库。我正在使用xampp
。我编写了PHP代码,用于获取Access表并创建查询字符串。当我执行我的程序时,它给了我以下错误:
内存不足(需要2465528字节)
我的代码是:
<?php
$conn=odbc_connect('my','','');/* connected */
$sql="SELECT * FROM mytable";
$rs=odbc_exec($conn,$sql);
$sqlstr="";
$tablefields=array("Name","City","State");
$sqlstr="INSERT INTO `mytable`(";
$temp=0;
for($i=0;$i<count($tablefields)-1;$i++)
{
$sqlstr.="`".$tablefields[$i]."`";
if($temp==count($tablefields)-2)
{
}
else
{
$sqlstr.=",";
$temp++;
}
}
$sqlstr.=") VALUES";
//echo $sqlstr;
//return;
$sqlstr.="(";
while($row = odbc_fetch_array($rs))
{
$temp=0;
foreach ($row as $key => $value)
{
$sqlstr.="'".$value."'";
if($temp==count($row)-1)
{
$sqlstr.="),(";
}else
{
$sqlstr.=",";
$temp++;
}
//echo $sqlstr;
}
}
$sqlstr.=")";
connection();
$res=mysql_query($sqlstr)or die (mysql_error());
function connection()
{
$host="localhost";
$user="root";
$pass="";
$database="mydatabase";
$cnx=mysql_connect($host, $user, $pass);
mysql_connect($host, $user, $pass);
echo (mysql_error($cnx));
mysql_select_db($database, $cnx);
echo (mysql_error($cnx));
return $cnx;
}
?>
我确实更改了max_execution_time = 3600
中的memory_limit = -1
和php.ini
并在my.ini
key_buffer = 200M
sort_buffer_size = 200M
read_buffer = 200M
write_buffer = 200M
谁能告诉我问题出在哪里?
答案 0 :(得分:1)
对于处于类似情况的未来读者,很多更好的方法是使用预准备语句来执行INSERT操作。优点包括:
<?php
$tablefields=array("Name","City","State");
$myDb = new PDO('mysql:host=localhost:3307;dbname=myDb', 'root', 'whatever');
$myDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$mySt = $myDb->prepare("TRUNCATE TABLE mytable");
$mySt->execute();
$params = array_fill(0, count($tablefields), '?');
$sqlstr = "INSERT INTO mytable (`" . implode("`,`", $tablefields) .
"`) VALUES (" . implode(",", $params) . ")";
// i.e., INSERT INTO mytable (`Name`,`City`,`State`) VALUES (?,?,?)
$mySt = $myDb->prepare($sqlstr);
$accDb = new PDO('odbc:' .
'Driver={Microsoft Access Driver (*.mdb, *.accdb)};' .
'Dbq=C:\\Users\\Public\\Database1.accdb;');
$accDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqlstr = "SELECT [" . implode("],[", $tablefields) . "] FROM myTable";
// i.e., SELECT [Name],[City],[State] FROM myTable
$accSt = $accDb->prepare($sqlstr);
$accSt->execute();
$rowCount = 0;
while ($row = $accSt->fetch()) {
for ($i = 0; $i < count($tablefields); $i++) {
$params[$i] = $row[$tablefields[$i]];
}
$mySt->execute($params);
$rowCount++;
}
echo $rowCount . " row(s) copied.<br />";
答案 1 :(得分:0)
每隔几百条记录插入一次。这样的事情: -
<?php
$conn=odbc_connect('my','','');/* connected */
$sql="SELECT * FROM mytable";
$rs=odbc_exec($conn,$sql);
$sqlstr="";
$tablefields=array("Name","City","State");
$sqlhead="INSERT INTO `mytable`(`".implode("`,`", $tablefields)."`) VALUES ";
connection();
$sqlitem = array();
while($row = odbc_fetch_array($rs))
{
$temp=0;
if (count($sqlitem) > 255)
{
$res=mysql_query($sqlhead.implode(',', $sqlitem))or die (mysql_error());
$sqlitem = array();
}
$sqlitem[] = "('".implode("','",$row)."')";
}
if (count($sqlitem) > 0)
{
$res=mysql_query($sqlhead."(".implode('),(', $sqlitem).")")or die (mysql_error());
}
function connection()
{
$host="localhost";
$user="root";
$pass="";
$database="mydatabase";
$cnx=mysql_connect($host, $user, $pass);
mysql_connect($host, $user, $pass);
echo (mysql_error($cnx));
mysql_select_db($database, $cnx);
echo (mysql_error($cnx));
return $cnx;
}
?>
通过将插入移动到类(而不仅仅是使用数组),您可以更干净地完成此操作。传递每一行以插入类,如果当前总数高于X,则执行插入,并从类中的析构函数执行插入。如果你需要做更多的插入,节省必须在循环结束时决定。
注意,不推荐使用mysql_ *函数,你可能应该使用mysqli_ *函数。