如何发送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());