Recently wrote yo A program need to use C language to connect MySQL, is based on Ubuntu, I wrote the following code (which includes the UDP Protocol section) in fact, we are through the system comes with the header file through the SQL statement to the database operation, this should be familiar with the database language of the person is very simple
Attach the available code:
#include <stdio.h>/*these is 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/inch.h>#include<arpa/inet.h>#include<mysql/mysql.h>#definePort 50001/* port that'll be opened */#defineMaxdatasize/* 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";ConstUnsignedintdb_port=3306;Charsql[ +];voidGetmessageinsert (Charreceive_msg[]) {intI=0, res=0;Chartemp[ -];Char*p[ A];Char*buff;strcpy (temp,receive_msg); Buff=temp; for(i=0;i< A; 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>= One) Break;}//printf ("i=%d\n", I);if(i>= One&&STRCMP (p[0],head) = =0&&STRCMP (p[ One],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[Ten]); 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[Ten]); Res=mysql_query (Mysql_conn,sql); printf ("%s\n", SQL);if(!res) {//number of rows affected by outputprintf"Inserted%lu rows\n", (unsignedLong) Mysql_affected_rows (mysql_conn)); } Else{//Print out error codes and detailsfprintf (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,d ata_1 Int (4) unsigned not null,data_2 int (4) isn't 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) isn't Null,judge tinyint (1) Default 0,longitude decimal (11,8) not null,latitude Decima L (11,8) not null,primary key (number))", p[1]); Res=mysql_query (mysql_conn, SQL); }}main () {intSOCKFD;/*socket Descriptors*/structsockaddr_in server;/*server ' s address information*/structSOCKADDR_IN client;/*client ' s address information*/socklen_t sin_size;intnum;CharRecvmsg[maxdatasize];/*Buffer for message*/CharSendmsg[maxdatasize];CharCondition[] ="quit";//Open Databasemysql_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);}
After the code is finished, it starts compiling, but the problem arises.
[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 St ATUs
[email protected]:/home/mycprogrammer#
Of course, we'll change the way we compile:
Gcc-o Udp_save $ (mysql_config--cflags) udp_save.c $ (mysql_config--libs)
This can be done by compiling the
Ubuntu under C language connection MySQL