sqlite php获取无法识别的令牌:插入和参数化查询时出现“:”错误

时间:2013-06-08 13:18:50

标签: php sqlite

我第一次使用sqlite。

准备像

这样的查询字符串
$articleInsertQuery = "INSERT INTO Articles VALUES (?, ?, ?, ?, ?)", ($i, $title, $content, $date, 93,);

返回“Parse error”。我也试过没有传递像

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

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

知道我做错了吗?

1 个答案:

答案 0 :(得分:2)

@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();

希望这有帮助。