SQLite的原因一般错误:17数据库模式已更改

时间:2016-12-27 14:53:17

标签: php sqlite pdo

为什么在$stmt2之前声明准备好的声明VACUUM backup会导致General error: 17 database schema has changed错误?

<?php

require '../vendor/autoload.php';

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
set_time_limit(0);


$db = new \PDO("sqlite:".__DIR__."/../db/datalogger.db");
$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$db->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ);

print_r($db->query('SELECT version, polltimeDestination FROM config WHERE bogus=0')->fetch());

$stmt1=$db->prepare('UPDATE config SET polltimeDestination=? WHERE bogus=0');
$stmt1->execute([300]);

$db->exec('DELETE FROM backup');
$stmt2=$db->prepare('UPDATE config SET version=? WHERE bogus=0');
$db->exec('VACUUM backup');

//$stmt2=$db->prepare('UPDATE config SET version=? WHERE bogus=0');
$stmt2->execute([8]);

输出:

stdClass Object ( [version] => 8 [polltimeDestination] => 300 ) 
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 17 database schema has changed' in /var/www/html/test2.php:26 Stack trace: #0 /var/www/html/test2.php(26): PDOStatement->execute(Array) #1 {main} thrown in /var/www/html/test2.php on line 26

1 个答案:

答案 0 :(得分:2)

根据YourCommonSense的评论......

任何架构更改者(如添加或删除表或列)都会显着导致架构更改。使用真空还会导致架构更改。尝试执行模式更改之前定义的预准备语句将导致SQLite General error: 17 database schema has changed错误。