C传递字符串进行查询,并检查结果是否为空(C pass string to query and also check if result is empty)

系统教程 行业动态 更新时间:2024-06-14 16:57:39
C传递字符串进行查询,并检查结果是否为空(C pass string to query and also check if result is empty)

当使用C连接到数据库时,我需要将一个字符串传递给查询内部,但由于我很长一段时间没有使用C,所以我不知道正确的方法。 我尝试了以下但是这不起作用。

if (mysql_query(conn, "select * from t1 where c1 = %s and c2 = %s", sptr->name, sptr->desc)) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); }

还有什么是检查查询是否返回空结果集的最简单方法?

I need to pass a string to inside the query when connecting to a database using C, but since I have not been using C for a long time I don't know the correct way to do it. I tried the below but that doesn't work.

if (mysql_query(conn, "select * from t1 where c1 = %s and c2 = %s", sptr->name, sptr->desc)) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); }

also what is the easiest method to check whether the query returned an empty result set?

最满意答案

IIRC,你不能直接在mysql_query()函数中使用格式说明符和相应的参数。

你需要做的是

分配一个临时缓冲区。 使用snprintf()在缓冲区中打印查询命令 使用缓冲区作为第二个参数调用mysql_query() 。

IIRC, you cannot use format specifiers and corresponding arguments directly inside mysql_query() function.

What you need to do is

Allocate one temporary buffer. use snprintf() to print the query command in the buffer call mysql_query() with the buffer as second argument.

更多推荐

本文发布于:2023-04-13 12:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/d5330868d3dbd594dbd065a692a9eed3.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   为空   检查结果   pass   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!