sprintf(stmt,"select * from mytable where 字段1=%d and 字段2=%d and 字段3=%d", i,j,k); mysql_query(&mysql,stmt);
如果都是用变量,可以使用stl::map解决 map<string,string> m_data; m_data["id"]="123456"; 然后使用迭代器构造sql语句. string query="select * from mytable where "; map<string,string>::iterator it=m_data.begin(); while(it!=m_data.end()){ query+=it->first; query+="="; query+=it->second; it++; }不知道是不是你想要实现的功能.
char szSqlText[500]; sprintf(szSqlText,"select * from anchorid where xdid=%d and pointid=%d and xdupdw='up'",xd_id,anchorid1); mysql_query(sock,szSqlText); 我用的这个,写的应该对吧, 可还是不行啊 acmain大哥再帮帮忙,我就是要用这种语句实现的
looyao的好像很复杂,我看不懂 哈 我是菜鸟
char szSqlText[500]; sprintf(szSqlText,"select * from anchorid where xdid=%d and pointid=%d and xdupdw='up'",xd_id,anchorid1); printf("%s",szSqlText);mysql_query(sock,szSqlText);看一下你的SQL语句是什么? 另外建议你说明一下是什么不行? 编译出错?结果不对?
先从程序中读取所需要的变量 或者直接在程序中定义变量mysql> set @a=1; Query OK, 0 rows affected (0.00 sec)mysql> select @a; +------+ | @a | +------+ | 1 | +------+ 1 row in set (0.01 sec)
mysql_query(&mysql,stmt);
map<string,string> m_data;
m_data["id"]="123456";
然后使用迭代器构造sql语句.
string query="select * from mytable where ";
map<string,string>::iterator it=m_data.begin();
while(it!=m_data.end()){
query+=it->first;
query+="=";
query+=it->second;
it++;
}不知道是不是你想要实现的功能.
sprintf(szSqlText,"select * from anchorid where xdid=%d and pointid=%d and xdupdw='up'",xd_id,anchorid1);
mysql_query(sock,szSqlText);
我用的这个,写的应该对吧, 可还是不行啊 acmain大哥再帮帮忙,我就是要用这种语句实现的
sprintf(szSqlText,"select * from anchorid where xdid=%d and pointid=%d and xdupdw='up'",xd_id,anchorid1); printf("%s",szSqlText);mysql_query(sock,szSqlText);看一下你的SQL语句是什么?
另外建议你说明一下是什么不行? 编译出错?结果不对?
Query OK, 0 rows affected (0.00 sec)mysql> select @a;
+------+
| @a |
+------+
| 1 |
+------+
1 row in set (0.01 sec)
表中的字段其中有 xdid int(11) pointid int(11)和 xdupdw varchar(30)
要看的sql语句是我写数据库时用的吗?
编译没有错误但有好多警告:warning C4996:'sprintf' was declared derecated
也不出结果,好像运行到sprintf是就跳出了的感觉 会弹出对话框里面有break和 contiue
#include "stdafx.h"
#include "winsock2.h"
#include <windows.h>
#include <iostream>
#include <mysql.h>
#include <stdio.h>
#include <conio.h>
using namespace std;int _tmain(int argc, _TCHAR* argv[])
{
int anchorid1='1',anchorid2='2',xd_id='1';
char *host = "localhost";//数据库IP
char *user = "root";//数据库账号
char *pass = "";//数据库密码
char *db = "mapdata";//当前数据库
//数据类型声明 MYSQL *sock;//数据库连接指针声明
MYSQL_RES *results;//查询结果集声明
MYSQL_ROW record;//查询结果行声明
sock = mysql_init(0);//初始化指针
if (sock)
cout << "数据库程序初始化成功!" << endl;
else
{
cout << "数据库程序初始化失败!" << mysql_error(sock) << endl;
} //connection
if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))
cout << "数据库连接成功!" << endl;
else
{
cout << "数据库连接失败: " << mysql_error(sock) << endl;
}
mysql_set_character_set(sock,"gb2312");//设置当前字符为gb2312,支持中文
cout << "当前字符集为: " << mysql_character_set_name(sock) << endl; char szSqlText[500];
sprintf(szSqlText,"select * from anchorid where xdid=%d and pointid=%d and xdupdw='up'",xd_id,anchorid1);
mysql_query(sock,szSqlText); results=mysql_store_result(sock);
record = mysql_fetch_row(results);
float anchor_x,anchor_y,anchor_z;
cout<<"anchor_x="<<record[0]<<endl<<"anchor_y="<<record[1]<<endl<<"anchor_z="<<record[2]<<endl;
getchar();
}