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.更多推荐
发布评论