下面的代码将值插入到MYSQL数据库中。 如果我使用“sprintf(Query,”INSERT into t(d)values(%c)“,a);”它不会将单词hello插入到我的数据库中。我也尝试在上面的语句中使用(%s)。 (d)值(%d)“,b);”它将值1234插入到数据库中。使用C++将字符插入到MYSQL表中
发生了什么事?为什么不把它插入到我的数据库中的字符或字符串,但只有整数?
d列是我的数据库被定义为char类型。
谢谢。
#include "stdafx.h"
#include <stdio.h>
#include <mysql.h>
//#include <string>
//using std::string;
//#include <atlstr.h>
MYSQL *pConnection;
MYSQL_RES *pResult=NULL;
MYSQL_ROW Row;
char Query[256];
int main(int argc, char* argv[])
{
pConnection = mysql_init(NULL);
mysql_real_connect(pConnection,"localhost","root","y123","test",0,NULL,0);
char a[] ="Hello";
int b = 1234;
printf("%s", a);
sprintf(Query, "INSERT into t(d) values (%s)", a);
mysql_query(pConnection, Query);
}
你列什么类型你试图插入? – aambrozkiewicz 2012-03-12 14:20:12