Access数据安全迁移到SQLite3[PHP代码]
<?php
header("Content-type:text/html;charset=gb2312");
set_time_limit(300);
//access
$str = realpath("single.mdb");
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn->Open('Provider=Microsoft.Jet.OLEDB.4.0; Data Source='.$str);
//测试
/*$i=0;
$rs = $conn->Execute("SELECT * FROM All_Income");
while (!$rs->EOF){
$i++;
echo $rs->Fields['IncomeContent']->Value,'<br>';
if ($i>=5) break;
$rs->MoveNext();
}
exit;
*/
//sqlite3
$pdo = new PDO("sqlite:inout.db") or exit('connect sqlite error');
//all_income迁移
$d = time();
$i = 0;
$rs = $conn->Execute("SELECT * FROM All_Income");
$pdo->beginTransaction();
while (!$rs->EOF){
$i++;
$sql = 'insert into t_income(f_date,f_income_money,f_income_content,f_tips)values(:fdate,:fincomemoney,:fincomecontent,:ftips)';
$x = floatval($rs->Fields['IncomeMoney']->Value);
$aRs = array(
':fdate' => $rs->Fields['DateID']->Value,
':fincomemoney' => sprintf('%.2f',$x),
':fincomecontent'=> mb_convert_encoding($rs->Fields['IncomeContent']->Value,'utf-8', 'gbk,utf-8'),
':ftips' => mb_convert_encoding($rs->Fields['Tips']->Value,'utf-8','gbk,utf-8'),
);
$s = $pdo->prepare($sql)->execute($aRs);
$rs->MoveNext();
}
$pdo->commit();
echo 'all_income迁移数据',$i,',用时'. (time() - $d).'秒</br>';
$rs->Close();
//all_outcome迁移
$d = time();
$i = 0;
$rs = $conn->Execute("SELECT * FROM All_Outcome");
$pdo->beginTransaction();
while (!$rs->EOF){
$i++;
$sql = 'insert into t_outcome(f_date,f_outcome_money,f_outcome_content,f_tips)values(:fdate,:foutcomemoney,:foutcomecontent,:ftips)';
$x = floatval($rs->Fields['OutcomeMoney']->Value);
$aRs = array(
':fdate' => $rs->Fields['DateID']->Value,
':foutcomemoney' => sprintf('%.2f',$x),
':foutcomecontent' => mb_convert_encoding($rs->Fields['OutcomeContent']->Value,'utf-8','gbk,utf-8'),
':ftips' => mb_convert_encoding($rs->Fields['Tips']->Value,'utf-8','gbk,utf-8'),
);
$s = $pdo->prepare($sql)->execute($aRs);
$rs->MoveNext();
}
$pdo->commit();
echo 'all_outcome迁移数据',$i,',用时'. (time() - $d).'秒</br>';
$rs->Close();
//
$conn->Close();
unset($rs,$conn,$pdo);
?>
all_income迁移数据51条,用时0秒
all_outcome迁移数据1206条,用时1秒
注意编码,不知道为什么在PHP下连接access时不能用utf-8编码,刚才不知道,结果导入的都是乱码。
建议:还是在ASP环境下转数据比较的可靠:1是时间格式,2不必要的麻烦产生,比如:要转换浮点数,不转的话,写到sqlite后会是一大串数字。
2。发现DateID到sqlite3后,变成了2011-8-7,这在access时怎样操作都行,但到了sqlite3后就要按字符串操作了,因为sqlite3的数据类型中并没有时间类型,而且可供操作的时间函数并不丰富,虽然julianday可供使用,但似乎比较的耗时。
1。Warning: number_format() expects parameter 1 to be double, object given in
答:$rs->Fields['OutcomeMoney']->Value这居然是一个对象,和上次取XML一样的,echo出来是正常的,就是不能直接使用PHP函数操作它,必须转一下才行。
header("Content-type:text/html;charset=gb2312");
set_time_limit(300);
//access
$str = realpath("single.mdb");
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn->Open('Provider=Microsoft.Jet.OLEDB.4.0; Data Source='.$str);
//测试
/*$i=0;
$rs = $conn->Execute("SELECT * FROM All_Income");
while (!$rs->EOF){
$i++;
echo $rs->Fields['IncomeContent']->Value,'<br>';
if ($i>=5) break;
$rs->MoveNext();
}
exit;
*/
//sqlite3
$pdo = new PDO("sqlite:inout.db") or exit('connect sqlite error');
//all_income迁移
$d = time();
$i = 0;
$rs = $conn->Execute("SELECT * FROM All_Income");
$pdo->beginTransaction();
while (!$rs->EOF){
$i++;
$sql = 'insert into t_income(f_date,f_income_money,f_income_content,f_tips)values(:fdate,:fincomemoney,:fincomecontent,:ftips)';
$x = floatval($rs->Fields['IncomeMoney']->Value);
$aRs = array(
':fdate' => $rs->Fields['DateID']->Value,
':fincomemoney' => sprintf('%.2f',$x),
':fincomecontent'=> mb_convert_encoding($rs->Fields['IncomeContent']->Value,'utf-8', 'gbk,utf-8'),
':ftips' => mb_convert_encoding($rs->Fields['Tips']->Value,'utf-8','gbk,utf-8'),
);
$s = $pdo->prepare($sql)->execute($aRs);
$rs->MoveNext();
}
$pdo->commit();
echo 'all_income迁移数据',$i,',用时'. (time() - $d).'秒</br>';
$rs->Close();
//all_outcome迁移
$d = time();
$i = 0;
$rs = $conn->Execute("SELECT * FROM All_Outcome");
$pdo->beginTransaction();
while (!$rs->EOF){
$i++;
$sql = 'insert into t_outcome(f_date,f_outcome_money,f_outcome_content,f_tips)values(:fdate,:foutcomemoney,:foutcomecontent,:ftips)';
$x = floatval($rs->Fields['OutcomeMoney']->Value);
$aRs = array(
':fdate' => $rs->Fields['DateID']->Value,
':foutcomemoney' => sprintf('%.2f',$x),
':foutcomecontent' => mb_convert_encoding($rs->Fields['OutcomeContent']->Value,'utf-8','gbk,utf-8'),
':ftips' => mb_convert_encoding($rs->Fields['Tips']->Value,'utf-8','gbk,utf-8'),
);
$s = $pdo->prepare($sql)->execute($aRs);
$rs->MoveNext();
}
$pdo->commit();
echo 'all_outcome迁移数据',$i,',用时'. (time() - $d).'秒</br>';
$rs->Close();
//
$conn->Close();
unset($rs,$conn,$pdo);
?>
all_income迁移数据51条,用时0秒
all_outcome迁移数据1206条,用时1秒
注意编码,不知道为什么在PHP下连接access时不能用utf-8编码,刚才不知道,结果导入的都是乱码。
2。发现DateID到sqlite3后,变成了2011-8-7,这在access时怎样操作都行,但到了sqlite3后就要按字符串操作了,因为sqlite3的数据类型中并没有时间类型,而且可供操作的时间函数并不丰富,虽然julianday可供使用,但似乎比较的耗时。
1。Warning: number_format() expects parameter 1 to be double, object given in
答:$rs->Fields['OutcomeMoney']->Value这居然是一个对象,和上次取XML一样的,echo出来是正常的,就是不能直接使用PHP函数操作它,必须转一下才行。
- 没有相关文章
- 没有评论