ubyntu 链接mysql_Ubuntu下C语言连接MySQL

#include /*These are the usual header files*/#include#include /*for close()*/#include#include#include#include#include#include

#define PORT 50001 /* Port that will be opened */

#define MAXDATASIZE 100 /* Max number of bytes of data */MYSQL*mysql_conn;char *head="head";char *drift="drift";const char *host_name="localhost";const char *user_name="user";const char *password="password";const char *db_name="dbname";const unsigned int db_port=3306;char sql[512];void getMessageInsert(charreceive_msg[])

{int i=0,res=0;char temp[100];char *p[12];char *buff;

strcpy(temp,receive_msg);

buff=temp;for(i=0;i<12;i++)p[i]=NULL;//printf("%s\n",buff);

char *token=strtok(buff,",");

p[0]=token;

i=0;while(token!=NULL)

{//printf("%s,i=%d\t",p[i],i);

token=strtok(NULL,",");

p[++i]=token;if(i>=11)break;

}//printf("i=%d\n",i);

if(i>=11&&strcmp(p[0],head)==0&&strcmp(p[11],drift)==0)

{//printf("%d,%d",strcmp(p[0],head),i);//printf("saved\n");

sprintf(sql,"insert into he%s(data_1,data_2,data_3,data_4,data_5,data_6,data_7,judge,longitude,latitude)values(%s,%s,%s,%s,%s,%s,%s,0,%s,%s)",p[1],p[2],p[3],p[4],p[5],p[6],p[7],p[8],p[9],p[10]);

sprintf(sql,"insert into he%s(data_1,data_2,data_3,data_4,data_5,data_6,data_7,judge,longitude,latitude)values(%s,%s,%s,%s,%s,%s,%s,0,%s,%s)",p[1],p[2],p[3],p[4],p[5],p[6],p[7],p[8],p[9],p[10]);

res=mysql_query(mysql_conn,sql);

printf("%s\n",sql);if (!res) { //输出受影响的行数

printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(mysql_conn));

}else { //打印出错误代码及详细信息

fprintf(stderr, "Insert error %d: %sn",mysql_errno(mysql_conn),mysql_error(mysql_conn)); sprintf(sql,"create table he%s(number int(9) unsigned not null auto_increment,p_data timestamp not null default current_timestamp,data_1 int(4) unsigned not null,data_2 int(4) not null,data_3 int(4) not null,data_4 int(4) not null,data_5 int(4) not null,data_6 int(4) not null,data_7 int(4) not null,judge tinyint(1) default 0,longitude decimal(11,8) not null,latitude decimal(11,8) not null,primary key(number))",p[1]);

res=mysql_query(mysql_conn, sql);

}

}

main()

{int sockfd; /*socket descriptors*/

struct sockaddr_in server; /*server's address information*/

struct sockaddr_in client; /*client's address information*/socklen_t sin_size;intnum;char recvmsg[MAXDATASIZE]; /*buffer for message*/

charsendmsg[MAXDATASIZE];char condition[] = "quit";//打开数据库

mysql_conn=mysql_init(NULL);if(!mysql_real_connect(mysql_conn, host_name, user_name, password, db_name, db_port, NULL, 0))

{

printf("connect error");

exit(1);

}/*Creating UDP socket*/

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {/*handle exception*/perror("Creating socket failed.");

exit(1);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值