我有以下查询存储在缓冲区中,然后使用我自己的conn.executeQuery()执行。
[uwsgi]
socket = 127.0.0.1:3031
wsgi-file = /home/axial/axial/axial/wsgi.py
workers = 10
virtualenv = /home/axial/venv/
pythonpath = /home/axial/venv/bin/python3
#home = /home/axial/venv/
#chdir = /home/axial/venv/
虽然这种方法很好,但我想知道它是否可以简化为类似......
char buffer[QUERY_MAX];
snprintf(buffer, QUERY_MAX, "SELECT * FROM players WHERE player_id = %d", 1);
conn.executeQuery(buffer);
我的功能:
conn.executeQuery("SELECT * FROM players WHERE player_id = "%d", 1);
我了解varargs,并尝试按照此处的示例(Variable number of arguments in C++?),但我不仅仅是尝试阅读varargs,而是将它们包含在查询中,对我来说显然很麻烦。
欢迎任何想法,谢谢。
答案 0 :(得分:1)
您需要一个准备好的声明,如下所示
MYSQL_STMT *stmt;
MYSQL_BIND params[1 /* Here it will be the number of arguments */];
MYSQL_BIND result[1 /* Here it will be the number of columns in the result row */];
int value;
const char *query;
int id;
memset(params, 0, sizeof params);
memset(result, 0, sizeof result);
// Assuming `mysql' is an initialized MYSQL object
stmt = mysql_stmt_init(mysql);
if (stmt == NULL)
return
// `SELECT ID' just to illustrate how you can select an integer
// value
query = "SELECT ID FROM players WHERE player_id = ?";
if (mysql_stmt_prepare(stmt, query, strlen(query)) != 0)
goto error;
value = 1;
result[0].buffer_type = MYSQL_TYPE_LONG;
result[0].buffer = &id;
params[0].buffer_type = MYSQL_TYPE_LONG;
params[0].buffer = &value;
if (mysql_stmt_bind_param(stmt, params) != 0)
goto error;
if (mysql_stmt_bind_result(stmt, result) != 0)
goto error;
if (mysql_stmt_execute(stmt) != 0)
goto error;
if (mysql_stmt_fetch(stmt) != 0)
goto error;
// Now all the columns are in the buffers of `result'
// or the bound variables (which is why we pass their address)
fprintf(stdout, "%d\n", id);
error:
mysql_stmt_close(stmt);