c++向mysql中插入变量数据

9 篇文章 0 订阅

#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;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值