如何使用 SQLITE3 LIKE 语句

发布于 2024-08-29 03:48:23 字数 695 浏览 5 评论 0原文

我在项目中运行动态 LIKE 语句时遇到问题: 这个查询的工作方式就像一个魅力,并返回名称中带有“t”的所有项目:

const char *sql = "select * from bbc_ipad_v1_node where name LIKE '%%t%%'";

当我尝试动态执行此操作时,我不会收到错误,而只是一个空结果。该值似乎为空。我尝试绑定一个输出正确值的字符串值's'

NSLog(@"bbc_ : search menu items from db based on: %@",s);
const char *sql = "select * from bbc_ipad_v1_node where name LIKE '%%?%%'";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
sqlite3_bind_text(statement, 1, [s UTF8String],-1,SQLITE_TRANSIENT);

我应该如何绑定这个值而不是使用:

const char *sql = "select * from bbc_ipad_v1_node where name LIKE '%%?%%'";

I have problems running a dynamic LIKE statement in my project:
this query works like a charm and returns all items with a 't' in their name:

const char *sql = "select * from bbc_ipad_v1_node where name LIKE '%%t%%'";

When I try to do this dynamically I do not get errors, but just an empty result. It seems that the value is null. I try to bind a string value 's' which outputs a correct value

NSLog(@"bbc_ : search menu items from db based on: %@",s);
const char *sql = "select * from bbc_ipad_v1_node where name LIKE '%%?%%'";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
sqlite3_bind_text(statement, 1, [s UTF8String],-1,SQLITE_TRANSIENT);

How should I bind this value instead of using:

const char *sql = "select * from bbc_ipad_v1_node where name LIKE '%%?%%'";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱人如己 2024-09-05 03:48:23

刚刚在 http: //www.innerexception.com/2008/10/using-like-statement-in-sqlite-3-from.html

我更改了以下几行:

const char *sql = "select * from bbc_ipad_v1_node where name LIKE ?001";

NSString *searchInput = [NSString stringWithFormat:@"%@%%", s];
sqlite3_bind_text(statement, 1, [searchInput UTF8String],-1,SQLITE_TRANSIENT); 

Just found a great explanation at http://www.innerexception.com/2008/10/using-like-statement-in-sqlite-3-from.html

I changed following lines:

const char *sql = "select * from bbc_ipad_v1_node where name LIKE ?001";

and

NSString *searchInput = [NSString stringWithFormat:@"%@%%", s];
sqlite3_bind_text(statement, 1, [searchInput UTF8String],-1,SQLITE_TRANSIENT); 
醉态萌生 2024-09-05 03:48:23

实际上你可以直接说

const char *sql = "select * from bbc_ipad_v1_node where name LIKE '%t%'";

(注意单%s)

You can actually just say

const char *sql = "select * from bbc_ipad_v1_node where name LIKE '%t%'";

(Note the single-%s)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文