如果我想用select *  from mytable where 字段1=     and    字段2=     and    字段3=其中字段1,2,3都要求从已定义的变量中取,要怎么做?   

解决方案 »

  1.   

    sprintf和mysql_query似乎都不行啊 请各位帮帮忙
      

  2.   

    sprintf(stmt,"select *  from mytable where 字段1=%d   and  字段2=%d   and  字段3=%d", i,j,k);
    mysql_query(&mysql,stmt);
      

  3.   

    如果都是用变量,可以使用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++;
    }不知道是不是你想要实现的功能.
      

  4.   

    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大哥再帮帮忙,我就是要用这种语句实现的
      

  5.   

    looyao的好像很复杂,我看不懂 哈  我是菜鸟
      

  6.   

    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语句是什么?
    另外建议你说明一下是什么不行? 编译出错?结果不对?
      

  7.   

    先从程序中读取所需要的变量 或者直接在程序中定义变量mysql> set @a=1;
    Query OK, 0 rows affected (0.00 sec)mysql> select @a;
    +------+
    | @a   |
    +------+
    |    1 |
    +------+
    1 row in set (0.01 sec)
      

  8.   

    我的数据库里的表名是anchorid
    表中的字段其中有 xdid  int(11)   pointid  int(11)和  xdupdw  varchar(30)
    要看的sql语句是我写数据库时用的吗?
    编译没有错误但有好多警告:warning C4996:'sprintf' was declared derecated
    也不出结果,好像运行到sprintf是就跳出了的感觉 会弹出对话框里面有break和 contiue
      

  9.   

    我的c++程序时这样写的#include <string>
    #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();
    }
      

  10.   

    C++ 中你不要用 sprintf 了。直接用字符串+吧string szSqlText= "select *  from anchorid where xdid=" + xd+id ...
      

  11.   

    这不是MYSQL上的问题,是你C++语言上的问题,建议你可以到C++的版块得到更准确和详细的帮助。