c++向mysql通信_C++向MySql数据库写数据

#includeusing namespace std;

int main()

{

if (mysql_library_init(0, NULL, NULL))

{

printf("could not initialize MySQL library\n");

getchar();

exit(1);

}

MYSQL mydata;

mysql_init(&mydata);

MYSQL *ret = mysql_real_connect(&mydata, "127.0.0.1", "root", "123456", "test", 0, NULL, 0);

if (!ret)

{

printf("Failed to connect to database:%s\n", mysql_error(&mydata));

getchar();

exit(1);

}

int i;

while (cin >> i)

{

time_t t ;

struct tm current_time;

time(&t); //获取系统日期和时间

localtime_s(¤t_time, &t); //获取当地日期和时间

string str;

int year = 1900 + current_time.tm_year;

int month = 1 + current_time.tm_mon;

int day = 1 + current_time.tm_mday;

int hour = current_time.tm_hour;

int min = current_time.tm_min;

int sec = current_time.tm_sec;

int type = 1;

string pass_rate = "true";

double diatance_move = 1.0 / i;

str = "insert into weldingworkpiece VALUES (";

str += to_string(year);

str += ",";

str += to_string(month);

str += ",";

str += to_string(day);

str += ",";

str += to_string(hour);

str += ",";

str += to_string(min);

str += ",";

str += to_string(sec);

str += ",'D:\\/吉纳尔\\/";

str += to_string(year);

str += "_";

str += to_string(month);

str += "_";

str += to_string(day);

str += "_";

str += to_string(hour);

str += "_";

str += to_string(min);

str += "_";

str += to_string(sec);

str += ".bmp',";

str += "";

str += to_string(type); //型号

str += ",";

str += to_string(1); //是否合格

str += ",";

str += to_string(diatance_move); //偏移量

str += ");";

//cout << str.c_str() << endl;

//const char* strl= str.c_str();

int ret1 = mysql_query(&mydata, str.c_str());

if (ret1 != 0)

{

printf("error:%s\n", mysql_error(&mydata));

getchar();

system("pause");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值