如何发送C ++和mysql动态mysql查询 [英] How to send C++ and mysql dynamic mysql queries
本文介绍了如何发送C ++和mysql动态mysql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Visual Studio,Windows 7和mysql.h库。
Working with Visual Studio, Windows 7 and mysql.h library.
我想要做的是发送一个MySQL查询:
What I want to do is send a MySQL query like this:
mysql_query(conn, "SELECT pass FROM users WHERE name='Leo Tolstoy'");
我唯一不能工作的是发送一个查询,如上所示,但是从文本字段或任何其他内容获取的变量。所以我应该如何使用变量而不是常量?
The only thing I can't get working is sending a query where the name would be not a constant as it's shown above, but a variable taken from a text field or anything else. So how should I work with a variable instead of a constant?
希望我的问题清楚。
推荐答案
这里是一个例子:
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
/// ...
string name_value = "Leo Tolstoy";
ostringstream strstr;
strstr << "SELECT pass FROM users WHERE name='" << name_value << "'";
string str = strstr.str();
mysql_query(conn, str.c_str());
这篇关于如何发送C ++和mysql动态mysql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文