Ubuntu下C語言串連MySQL

來源:互聯網
上載者:User

標籤:方式   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);}

 

程式碼完成之後就開始編譯了,不過問題出現了

[email protected]:/home/myCProgrammer# vim udp_save.c
[email protected]:/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
[email protected]:/home/myCProgrammer#

當然,我們換一種編譯方式:
 gcc -o udp_save $(mysql_config --cflags) udp_save.c $(mysql_config --libs)
這樣就可以通過編譯了

 

Ubuntu下C語言串連MySQL

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.