64,654
社区成员
发帖
与我相关
我的任务
分享
sql+= std::string("0x") + int_to_str;
sql+= int_to_str;
mysql_query(conn,sql.c_str()); // now it works...
#include <string> // for c++ std string
...
std::string sql="select rt2 from dt where rt1=";
int ty=9;
char int_to_str[9]="";
sprintf(int_to_str,"%x",ty); // int_to_str中能存放一个任意4字节int型
sql+= "0x" + int_to_str;
mysql_query(conn,sql.c_str()); // now it works?
...
#include <fstream.h>
#include <windows.h>
#include <cstdlib>
#include <cstdio>
#include "mysql.h"
#pragma comment(lib, "libmysql.lib")
MYSQL * conn;
#include <algorithm>
using namespace std;
int main()
{
char host[] = "localhost";
char username[] = "root";
char password[] = "123456";
char database[] = "tt";
conn = mysql_init(NULL);
if(!mysql_real_connect(conn,host,username,password,database,0, NULL, 0))
{
cout<<"cannot connect mysql"<<endl;
return 0;
}
MYSQL_RES *res_set;
MYSQL_ROW row;
unsigned int i, ret;
MYSQL_FIELD *field;
unsigned int num_fields;
int ty = 9;
char sql[1024];
memset(sql, 0x00, 1024);
sprintf(sql, "select rt2 from dt where rt1 = %d", ty );
//size_t len = strlen(sql);
//sql[strlen(sql)] = '\0';
mysql_real_query(conn, sql );
res_set = mysql_store_result(conn);
row = mysql_fetch_row(res_set);
cout <<row[0] <<endl;
return 0;
}
std::string ty("9");
std::string sql("select rt2 from dt where rt1 =");
sql = sql+ty;
#include <string> // for c++ std string
...
std::string sql="select rt2 from dt where rt1=";
int ty=9;
char int_to_str[5]="";
sprintf(int_to_str,"%d",ty);
sql+= sql + int_to_str;
mysql_query(conn,sql); // maybe it work...
...