sqlite与php同时读写

时间:2018-03-21 14:20:20

标签: php sqlite

我想在阅读后写入sqlite数据库。 这是两个sql语句, 1)我想读一些数据

Select animalName ,longitude, latitude , cast(((strftime('%s', CURRENT_TIMESTAMP ) - strftime('%s', time)) /(60  )) as timeDiff) as diff from Animal where localStatus=='locallyDisplayed' and diff > 30 ";

2)然后我想更新那些选定的数据

UPDATE Animal SET localStatus='notlocallyDisplayed' where localStatus=='locallyDisplayed' and cast(((strftime('%s', CURRENT_TIMESTAMP ) - strftime('%s', time)) /(60  )) as timeDiff) > 30  ";

我得到一个sqlite" 数据库锁定错误"在尝试写(UPDATE)时。请你建议一个更好的方法来处理这个错误。如何在读取(SELECT)后解锁数据库,以便我可以成功写入数据库(UPDATE)。我使用php作为服务器端语言。

以下是代码:

    $conn1 = new SQLite3('mydatabase.sqlite');

$sql1 = "Select animalName ,longitude, latitude , cast(((strftime('%s', CURRENT_TIMESTAMP ) - strftime('%s', time)) /(60  )) as timeDiff) as diff from Animal where localStatus=='locallyDisplayed' and diff > 30 ";
$sql2 = "UPDATE Animal SET localStatus='notlocallyDisplayed' where localStatus=='locallyDisplayed' and cast(((strftime('%s', CURRENT_TIMESTAMP ) - strftime('%s', time)) /(60  )) as timeDiff) > 30  ";

$result = $conn1->query($sql1);

$myArray = array(); 
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {

    $myArray[] = $row;

}


$conn1->exec($sql2);
$conn1->close();
echo json_encode($myArray);

0 个答案:

没有答案