c++向mysql中插入變量數據

#include<netinet/in.h> // sockaddr_in

#include<sys/types.h> // socket

#include<sys/socket.h> // socket

#include<arpa/inet.h>

#include<unistd.h>

#include<sys/select.h> // select

#include<sys/ioctl.h>

#include<sys/time.h>

#include<iostream>

#include<vector>

#include<map>

#include<string>

#include <string.h>

#include<cstdlib>

#include<cstdio>

#include<cstring>

#include "ros/ros.h"

#include <geometry_msgs/Twist.h>

#include "agv_server/mmysql.h"

using namespace std;

float x = 0.0, y = 0.0, z = 0.0;

string inttostring(int in)

{

stringstream ss;

string str;

ss << in;

ss >> str;

return str;

}

void getUwb_xyzCallback(const geometry_msgs::Twist& vel_cmd) {

x = vel_cmd.linear.x;

y = vel_cmd.linear.y;

z = vel_cmd.linear.z;

//cout << "x=" << x << "," << "y=" << y << "," << "z=" << z << "," << endl;

}

int main(int argc, char **argv) {

ros::init(argc, argv, "agv_mysql_insert");

ros::NodeHandle nh;

ros::Subscriber recvxyz = nh.subscribe("uwb/cmd_vel", 100,getUwb_xyzCallback);

mmysql *pInstall = mmysql::makeInstall();

pInstall->mysql_connect("root", "nyy123456", "uwb");

while (ros::ok) {

time_t tt = time(NULL); //這句返回的只是一個時間cuo

tm* t = localtime(&tt);

string mytime =inttostring(t->tm_year + 1900) + "-" + inttostring(t->tm_mon + 1) + "-" + inttostring(t->tm_mday)+ " " + inttostring(t->tm_hour) + ":" + inttostring(t->tm_min) + ":" + inttostring(t->tm_sec);

cout << mytime << endl;

char sdate[30];

strcpy(sdate, mytime.c_str());

char mysql[] = "insert into uwb_area_beacon_real (id,beacon_id,coordinate_x,coordinate_y,coordinate_z,time) values(null,%d,%f,%f,%f,'%s')";

char mysqlBuf[1024];

sprintf(mysqlBuf, mysql,1,x,y,z, sdate);

pInstall->mysql_insert(mysqlBuf);

ros::spinOnce();

ros::Rate rate(1000);

rate.sleep();

}

return 0;

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章