码迷,mamicode.com
首页 > 数据库 > 详细

Ubuntu下C语言连接MySQL

时间:2017-09-05 00:07:40      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:方式   collect   root   splay   undefined   data   string   void   connect   

 

最近写了哟个程序需要用C语言连接MySQL,是基于Ubuntu的,我就写了如下的代码(其中包括了UDP协议部分)

事实上我们就是通过系统自带的头文件通过SQL语句对数据库进行操作,这应该对熟悉数据库语言的人就非常简单了

附上可用的代码:

#include <stdio.h> /* These are the usual header files */
#include <string.h>
#include <unistd.h> /* for close() */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <mysql/mysql.h>
 
#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(char receive_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;
int num;
char recvmsg[MAXDATASIZE]; /* buffer for message */
char sendmsg[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);
}

 

代码完成之后就开始编译了,不过问题出现了

root@iZ28j1w4g5uZ:/home/myCProgrammer# vim udp_save.c
root@iZ28j1w4g5uZ:/home/myCProgrammer# gcc udp_save.c -o udp_save
/tmp/ccaY48dW.o: In function getMessageInsert‘:
udp_save.c:(.text+0x201): undefined reference to
mysql_query’
udp_save.c:(.text+0x229): undefined reference to mysql_affected_rows‘
udp_save.c:(.text+0x248): undefined reference to
mysql_error’
udp_save.c:(.text+0x257): undefined reference to mysql_errno‘
udp_save.c:(.text+0x2a8): undefined reference to
mysql_query’
/tmp/ccaY48dW.o: In function main‘:
udp_save.c:(.text+0x301): undefined reference to
mysql_init’
udp_save.c:(.text+0x354): undefined reference to `mysql_real_connect’
collect2: error: ld returned 1 exit status
root@iZ28j1w4g5uZ:/home/myCProgrammer#

当然,我们换一种编译方式:
 gcc -o udp_save $(mysql_config --cflags) udp_save.c $(mysql_config --libs)
这样就可以通过编译了

 

Ubuntu下C语言连接MySQL

标签:方式   collect   root   splay   undefined   data   string   void   connect   

原文地址:http://www.cnblogs.com/h1994zw/p/7476321.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!