【问题标题】:sqlite php getting unrecognized token: ":" error while inserting and parameterized querysqlite php在插入和参数化查询时获取无法识别的令牌:“:”错误
【发布时间】:2013-06-08 13:18:49
【问题描述】:

我是第一次使用 sqlite。

准备一个类似的查询字符串

$articleInsertQuery = "INSERT INTO Articles VALUES (?, ?, ?, ?, ?)", ($i, $title, $content, $date, 93,);

它返回“解析错误”。我也尝试过不通过像

这样的参数化查询
$articleInsertQuery = "INSERT INTO Articles VALUES ($i, $title, $content, $date, 93)";

得到"Unable to prepare statement: 1, unrecognized token: ":" "

知道我哪里做错了吗?

【问题讨论】:

  • 您是否使用任何功能来做到这一点?如果是这样,请告诉我们。
  • $query = $dbObject->prepare($articleInsertQuery); $query->execute();
  • 嘿@arnold,来自哪个班级?
  • class AppDBClass extends SQLite3{ function __construct() { $this->open('../Server/db/sqlite3_database.db'); } } 和 $dbObject = new AppDBClass();

标签: php sqlite


【解决方案1】:

@arnold如果您为此使用 PDO。

prepare 并执行查询的方式如下。

$dbObject = new PDO('sqlite:sqlitedb');// NEW LINE
$articleInsertQuery = "INSERT INTO Articles VALUES (?, ?, ?, ?, ?)";
$query = $dbObject->prepare($articleInsertQuery);
$query->execute(array($i, $title, $content, $date, 93));

编辑:

sqlite3 prepare

$articleInsertQuery = "INSERT INTO Articles VALUES (:i, :title, :content, :date, :int)";
$query = $dbObject->prepare($articleInsertQuery);
$query->bindValue(':i', $i, SQLITE3_INTEGER);
$query->bindValue(':title', $title, SQLITE3_TEXT);
$query->bindValue(':content', $content, SQLITE3_TEXT);
$query->bindValue(':date', $date, SQLITE3_TEXT);
$query->bindValue(':int', 93, SQLITE3_INTEGER);    

$result = $query->execute();

希望这会有所帮助。

【讨论】:

  • 对于第一个解决方案,它返回 PHP 警告:SQLite3Stmt::execute() 需要 0 个参数,给定 1 个
  • @arnold... 试试下面那个。
  • 会像 $query->execute();对吗?
  • 是的,对不起...复制/粘贴问题。
  • 我得到这个 SQLite3Stmt::execute(): Unable to execute statement: constraint failed
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多