简体   繁体   English

PDO SQLite无法更新记录:“数据库被锁定”

[英]PDO SQLite cannot update record: “database is locked”

I'm struggling with this all day. 我整天都在苦苦挣扎。 I've read numerous posts, tried all suggestions but nothing works. 我阅读了很多帖子,尝试了所有建议,但没有任何作用。

This is my PHP code: 这是我的PHP代码:

try {
$db = new PDO('sqlite:test.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$result = $db->query('SELECT * FROM v_test');

foreach ($result as $row) {
  echo $row['column1'] . " | " . $row['column2'] . "<br>";

  /**************************************
  * Update table                        *
  **************************************/
  if (!$db->exec("update test set column2 = date('now') where column1 ='" . $row['column1'] . "';") === TRUE) {
    echo "Cannot update date:" . $db->lastErrorMsg();
    $db = null;
    exit;
  } 
}

/**************************************
* Close db connections                *
**************************************/
$db = null;
}
catch(PDOException $e) {
  echo "PDOException: " . $e->getMessage() . "<br>";

  /*** show the error info ***/
  foreach($db->errorInfo() as $error)
  {
    echo $error.'<br />';
  }

  $db = null;  
}

I run PHP v5.3.3. 我运行PHP v5.3.3。 With just the loop after the select I get the correct values from the table, so I can access the database, which is in the same folder as my script. 只有选择后的循环我从表中获取正确的值,所以我可以访问数据库,该数据库与我的脚本位于同一文件夹中。 The folder has 0777 rights, the database and the script both have 0660, but I've also tried with 0777. But when I try to update a record I get the 'database is locked' error. 该文件夹有0777权限,数据库和脚本都有0660,但我也尝试过0777.但是当我尝试更新记录时,我得到'数据库被锁定'错误。

I've used the same database before on a different server, but not with PDO but with $db = new SQLite3('mailing.db', SQLITE3_OPEN_READWRITE); 我之前在不同的服务器上使用过相同的数据库,但不是使用PDO而是使用$db = new SQLite3('mailing.db', SQLITE3_OPEN_READWRITE); I couldn't use the same script because SQLITE isn't enabled on the new server, but PDO_SQLITE is. 我无法使用相同的脚本,因为新服务器上没有启用SQLITE,但是PDO_SQLITE是。

My phpinfo() says: 我的phpinfo()说:

  • PDO drivers mysql, odbc, pgsql, sqlite PDO驱动程序mysql,odbc,pgsql,sqlite
  • pdo_sqlite. PDO_SQLITE。 PDO Driver for SQLite 3.x enabled SQLite Library 3.3.6 SQLite 3.x的PDO驱动程序启用了SQLite Library 3.3.6
  • '--without-sqlite' '--without-sqlite的'
  • '--without-sqlite3' '--without-sqlite3的'

Of course I tried to enable SQLite on the new server first so I could use the original script. 当然我首先尝试在新服务器上启用SQLite,这样我就可以使用原始脚本了。 But because I'm not a system engineer (just a developer ;)) I had hoped I could use the PDO option. 但是因为我不是系统工程师(只是开发人员;))我曾希望我可以使用PDO选项。

Is my problem related to my PHP configuration or is my script wrong? 我的问题与我的PHP配置有关,还是我的脚本错了?

Here is a shortened version of one way to handle queries so that the connection is closed each time a query is run, freeing up SQLite for the next query: 以下是处理查询的一种缩短版本,以便每次运行查询时都关闭连接,从而为下一个查询释放SQLite:

define("DBC", "sqlite:database_name.db");

/*
 * dataQuery($query) - one argument (required), a query string
 * generic query function where the query must be specified in source where data is required e.g,
 *
 *      $getFoo = "SELECT `foo` FROM `bar` ORDER BY `glorp`";
 *      $results = dataQuery($getFoo);
 *
 * All functions forming a query utilize this single function to return the results of their queries. The database
 * connection is instantiated and then destroyed (when the script completes) within this function.
 */

function dataQuery($query)
{
    // establish database connection
    try
    {
        $db = new PDO(DBC);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e)
    {
        $errorMsg = $e->getMessage();
        return $errorMsg;
    }

    // try to run query
    try
    {
        $queryResults = $db->query($query);
        if($queryResults != null)
        {
            $results = $queryResults->fetchAll(PDO::FETCH_OBJ); // return an object, you can return an array
            $queryResults = NULL; // closes the connection
            return $results;
        }
    }
    catch(PDOException $e)
    {
        $errorMsg = $e->getMessage();
        return $errorMsg;
    }
}

Having done this we can now execute a query - 完成此操作后,我们现在可以执行查询 -

$query = "SELECT `foo` FROM `bar`";
$results = dataQuery($query);

We could loop through the results and send several updates to the database or whatever we need to do. 我们可以遍历结果并向数据库或我们需要做的任何事情发送几个更新。 The connection gets cleaned up each time (where it is set to NULL) as soon as the results are returned. 返回结果后,每次连接都会被清除(设置为NULL)。

The most notable benefit is that connecting to and returning data from the database is independent of the queries themselves, making the code much more modular and flexible. 最显着的好处是连接到数据库并从数据库返回数据独立于查询本身,使代码更加模块化和灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM