PHP代码
读入txt文件,并写入到sqlite数据库里
<?php date_default_timezone_set('PRC'); $pdo = new PDO('sqlite:db/qq71e.sqlite3'); $sql = array(); $sql[] = 'CREATE TABLE qq(qq text,mobile text)'; //$sql[] = 'CREATE INDEX idx_qq_qq ON qq(qq)'; //$sql[] = 'CREATE INDEX idx_qq_mobile ON qq(mobile)'; foreach ($sql as $sqlOne) { $pdo->query($sqlOne); } $f = fopen('db/qq71e.txt', 'r'); $i = 0; echo date('Y-m-d H:i:s') . " " . $i . " "; $pdo->query('BEGIN'); $sql = "INSERT INTO qq(qq,mobile)VALUES(:qq, :mobile)"; $sth = $pdo->prepare($sql); while (!feof($f)) { if ($i % 1000000 === 0) { echo date('Y-m-d H:i:s') . " " . $i . " ... "; $pdo->query('COMMIT'); echo "[OK] "; $pdo->query('BEGIN'); } $line = func1(fgets($f)); if ($line == '') continue; $row = explode('----', $line); $i++; $data = array(); $data[':qq'] = $row[0]; $data[':mobile'] = isset($row[1]) ? $row[1] : ''; $sth->execute($data); } function func1($str) { $str = trim($str . " "); $str = trim($str . " "); return $str; } $pdo->query('COMMIT'); echo date('Y-m-d H:i:s') . " " . $i . " ";
下面是查询数据是否有异常
<?php date_default_timezone_set('PRC'); $pdo = new PDO('sqlite:db/qq71e.sqlite3'); $sql = 'SELECT * FROM qq LIMIT 10'; //$sql = 'SELECT * FROM qq WHERE qq LIKE :qq LIMIT 10'; $sth = $pdo->prepare($sql); $param = array(); //$param[':qq'] = '30%'; $sth->execute($param); $result = $sth->fetchAll(PDO::FETCH_ASSOC); print_r($result);
还需要创建个索引来加快查询速度