c++ - WHERE 列 = 值,仅适用于 INTEGER 值

标签 c++ sql sqlite syntax-error

我在 C++ 项目上使用 sqlite,但是当我在具有文本值的列上使用 WHERE 时出现问题

我创建了一个 sqlite 数据库:

    CREATE TABLE User( id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(24))

当我尝试获取具有 VARCHAR 值的列的值时,它不起作用,并在 sqlite3_step 之后返回给我一个 STATUS_CODE 101 :

int res = 0;
sqlite3_stmt *request;
char *sqlSelection = (char *)"SELECT * FROM User WHERE name='bob' ";
int id = 0;

res = sqlite3_prepare_v2(db, sqlSelection, strlen(sqlSelection), &request, NULL);

if (!res){
while (res == SQLITE_OK || res == SQLITE_ROW){
  res = sqlite3_step(request);
  if (res == SQLITE_OK || res == SQLITE_ROW ){
    id = sqlite3_column_int(request, 0);
    printf("User exist %i \n",id);
  }
}
sqlite3_finalize(request);

我也试过 LIKE 但它也不起作用

SELECT * FROM User WHERE name LIKE '%bob%'

但是当我执行相同的代码但得到一个 INTERGER 值时

SELECT * FROM User WHERE id=1

它工作正常。

在 SQLite 的数据库浏览器中,所有请求都可以正常工作。

最佳答案

为了解决这个问题,我搜索了状态码 101 的含义。

这是他们说的。

(101) SQLITE_DONE

The SQLITE_DONE result code indicates that an operation has completed. The SQLITE_DONE result code is most commonly seen as a return value from sqlite3_step() indicating that the SQL statement has run to completion. But SQLITE_DONE can also be returned by other multi-step interfaces such as sqlite3_backup_step().

https://sqlite.org/rescode.html

所以,您得到 101,因为 SELECT SQL 没有更多结果。

关于c++ - WHERE 列 = 值,仅适用于 INTEGER 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58360676/

相关文章:

java - android java持久对象存储在sqlite数据库中

c++ - 如何处理具有 IWebBrowser2 对象的 IHttpSecurity::OnSecurityProblem

c++ - 指向模板参数类型的指针

mysql - 以字符串格式返回 MySQL 表中的所有列

sql - Django 在/不在查询中

mysql内连接限制结果

SQL 图效率

c++ - 如何读锁多线程C++程序

c++ - 具体功能作为默认模板参数

python - 在 Heroku 上使用 Python2.7 导入 sqlite3