- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
当使用准备好的语句在 PHP 中向 SQLite3 插入多行时,如果您没有为一行绑定(bind)参数,那么将插入前一行的值,即使您“清除”了行与行之间的语句。
看下面的例子:
$db = new SQLite3('dogsDb.sqlite');
//create the database
$db->exec("CREATE TABLE Dogs (Id INTEGER PRIMARY KEY, Breed TEXT, Name TEXT, Age INTEGER)");
$sth = $db->prepare("INSERT INTO Dogs (Breed, Name, Age) VALUES (:breed,:name,:age)");
$sth->bindValue(':breed', 'canis', SQLITE3_TEXT);
$sth->bindValue(':name', 'jack', SQLITE3_TEXT);
$sth->bindValue(':age', 7, SQLITE3_INTEGER);
$sth->execute();
$sth->clear(); //this is supposed to clear bindings!
$sth->reset();
$sth->bindValue(':breed', 'russel', SQLITE3_TEXT);
$sth->bindValue(':age', 3, SQLITE3_INTEGER);
$sth->execute();
尽管我希望第二行的“name”列具有 NULL 值,但该值是“jack”!
所以要么“清除”似乎不起作用(尽管它返回 true),要么我还没有真正理解它应该做什么。
如何清除 SQLite3(甚至 PDO)中插入之间的绑定(bind)?在某些行的某些字段可能具有空值的情况下插入多行的最佳方法是什么?
最佳答案
#include <sqlite3.h>
#include <stdio.h>
int main(void) {
sqlite3 *db;
char *err_msg = 0;
sqlite3_stmt *res;
sqlite3_stmt *res1;
int rc = sqlite3_open("test.sqlite", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
char *sql = "CREATE TABLE Dogs (Id INTEGER PRIMARY KEY, Breed TEXT, Name TEXT, Age TEXT)";
rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
if (rc == SQLITE_OK) {
rc = sqlite3_step(res);
}
sqlite3_finalize(res);
char *sql1 = "INSERT INTO Dogs (Breed, Name, Age) VALUES (:breed,:name,:age);";
rc = sqlite3_prepare_v2(db, sql1, -1, &res1, 0);
if (rc == SQLITE_OK) {
printf("%d\n", sqlite3_bind_text(res1, 1, "breed1", 6, SQLITE_STATIC));
sqlite3_bind_text(res1, 2, "name1", 5, SQLITE_STATIC);
sqlite3_bind_text(res1, 3, "age1", 4, SQLITE_STATIC);
printf("%d\n", sqlite3_step(res1));
} else {
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
}
sqlite3_reset(res1);
sqlite3_clear_bindings(res1);
printf("%d\n", sqlite3_bind_text(res1, 2, "name2", 5, SQLITE_STATIC));
printf("%d\n", sqlite3_bind_text(res1, 3, "age2", 4, SQLITE_STATIC));
printf("%d\n", sqlite3_step(res1));
sqlite3_finalize(res1);
sqlite3_close(db);
return 0;
}
关于php - 清除 SQLite3 语句上的绑定(bind)似乎不起作用 (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32917795/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!