使用php将大量行插入sqlite db

时间:2017-06-30 11:18:18

标签: php sqlite

我有以下PHP代码,10000-20000行正常工作,但如果我需要插入说300000000(300k)行o当然php会超时或客户端将关闭选项卡。 有什么建议如何改进代码?以下代码只是脚本的一部分。

$db = new SQLite3('my.db');
$db->busyTimeout(5000);
$db->exec("PRAGMA journal_mode=WAL;");
if($q == 'obserations' && isset($_POST['obserations'])){
$van = $db->query("SELECT name FROM sqlite_master;");
    while($array = $van->fetchArray(SQLITE3_ASSOC)){
        $mytables = trim($array['name']);

        $mylines = trim($_POST['obserations']);
        $mylines = explode("\r\n", $mylines);
        $count = 0;
        while(!empty($mylines[$count])){
            $new_obs = $mylines[$count];
            $new_obs = SQLite3::escapeString(htmlentities($new_obs, ENT_QUOTES, 'UTF-8'));
            if(!$db->querySingle("SELECT * FROM '$mytables' WHERE obs = '$new_obs';")){
                if(preg_match("~^http://$mytables/.*$~", $new_obs, $matches)){
                    $db->exec("INSERT INTO '$mytables' (obs, col1, col2, col3, col4, col5) VALUES ('$new_obs', '', '', '', '', '');");
                }
            }
            $count++;
        }
    }
}

0 个答案:

没有答案