dousong2967 2018-04-17 11:36
浏览 55
已采纳

PHP SQLite - 准备好的语句行为不端?

I have the following SQLite table

CREATE TABLE keywords
(
 id INTEGER PRIMARY KEY,
 lang INTEGER NOT NULL,
 kwd TEXT NOT NULL,
 count INTEGER NOT NULL DEFAULT 0,
 locs TEXT NOT NULL DEFAULT '{}'
);

CREATE UNIQUE INDEX  kwd ON keywords(lang,kwd);

Working in PHP I typically need to insert keywords in this table, or update the row count if the keyword already exists. Take an example

$langs = array(0,1,2,3,4,5);
$kwds = array('noel,canard,foie gras','','','','','');

I now these data run through the following code

 $len = count($langs);
 $klen = count($kwds);
 $klen = ($klen < $len)?$klen:$len;

 $sqlite = new SQLite3('/path/to/keywords.sqlite');
 $iStmt = $sqlite->prepare("INSERT OR IGNORE INTO keywords (lang,kwd) 
 VALUES(:lang,:kwd)");
 $sStmt = $sqlite->prepare("SELECT rowid FROM keywords WHERE lang = :lang 
 AND kwd = :kwd");

 if (!$iStmt || !$sStmt) return;

 for($i=0;$i < $klen;$i++)
 {
  $keywords = $kwds[$i];
  if (0 === strlen($keywords)) continue;
  $lang = intval($langs[$i]);


  $keywords = explode(',',$keywords);
  for($j=0;$j < count($keywords);$j++)
  {
   $keyword = $keywords[$j];
   if (0 === strlen($keyword)) continue;

   $iStmt->bindValue(':kwd',$keyword,SQLITE3_TEXT);
   $iStmt->bindValue(':lang',$lang,SQLITE3_INTEGER);
   $sStmt->bindValue(':lang',$lang,SQLITE3_INTEGER);
   $sStmt->bindValue(':kwd',$keyword,SQLITE3_TEXT);

   trigger_error($keyword);
   $iStmt->execute();
   $sqlite->exec("UPDATE keywords SET count = count + 1 WHERE lang = 
   '{$lang}' AND kwd = '{$keyword}';");

   $rslt = $sStmt->execute();
   trigger_error($sqlite->lastErrorMsg());
   trigger_error(json_encode($rslt->fetchArray()));
  }
 }

which generates the following trigger_error output

Keyword: noel Last error: not an error SELECT Result: {"0":1,"id":1}

Keyword: canard Last Error: not an error
SELECT Reult:false

Keyword:foiegras Last Error: not an error SELECT Result: false

From the SQLite command line I see that the three row entries are present and correct in the table with the id/rowid columns set to 1, 2 and 3 respectively. lastErrorMsg does not report an error and yet two of the three $rslt->fetchArray() statements are returning false as opposed to an array with rowid/id attributes. So what am I doing wrong here?


I investigated this a bit more and found the underlying case. In my original code the result from the first SQLite3::execute - $iStmt-execute() - was not being assigned to anything. I did not see any particular reason for fetching and interpreting that result. When I changed that line of code to read $rslt = $iStmt->execute() I got the expected result - the rowid/id of the three rows that get inserted was correctly reported.

It is as though internally the PHP SQLite3 extension buffers the result from SQLiteStatement::execute function calls. When I was skipping the assignment my next effort at running such a statement, $sStmt->execute() was in effect fetching the previous result. This is my interpretation without knowing the inner workings of the PHP SQLite3 extension. Perhaps someone who understands the extension better would like to comment.

  • 写回答

1条回答 默认 最新

  • dounai1986 2018-04-20 02:37
    关注

    Add $rslt = NONE; right after trigger_error(json_encode($rslt->fetchArray())); and the correct results appear.

    FetchArray can only be called once and somehow php is not detecting that the variable has changed. I also played with changing bindValue to bindParam and moving that before the loop but that is unrelated to the main issue.

    It is my opinion that my solution should not work unless there is a bug in php. I am too new at the language to feel confident in that opinion and would like help verifying it. Okay, not a bug, but a violation of the least surprise principle. The object still exists in memory so without finalizing it or resetting the variable, fetch array isn't triggering.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图