Linux下的mysql应用

来源:互联网 发布:平价好用的指甲油知乎 编辑:程序博客网 时间:2024/06/05 03:12

linux下mysql函数的详细案列

1 MYSQL  *    STDCALL mysql_real_connect(MYSQL *mysql, const char *host,2                                            const char *user,3                                            const char *passwd,4                                            unsigned int port,5                                            const char *unix_socket,6                                            unsigned int clientflag);

 

 
 
1.  如何连接数据mysql数据库...。
 
[gxjun@localhost Mysql]$ cat demo.c

                                                                                                                                                                                                

复制代码
 1 //connect to mysql 2 #include<stdio.h> 3 #include"mysql.h" 4                                                                                                                                                                                                  5 int main (void) 6 {                                                                                                                                                                                                7   MYSQL mysql;                  //need a instance to init 8                                                                                                                                                                                                  9   int t, r;                     //connect the database10                                                                                                                                                                                                 11   mysql_init (&mysql);12                                                                                                                                                                                                 13   if (!mysql_real_connect14       (&mysql, "localhost", "root", "123", "demo", 0, NULL, 0))15     {16       printf ("Error connecting to database:%s\n", mysql_error (&mysql));17     }18   else19     {20       printf ("Connected MYSQL successfully!\n");21     }22   mysql_close (&mysql);23   return 0;24 }25 26  
复制代码

 

2.关于make文件的内容:
 
[gxjun@localhost Mysql]$ cat demo.mk
复制代码
 1 .SUFFIXES: .o .c 2                                                                                                                                                                                                 3 CC = gcc 4 SRC = demo.c 5 OBJS = $(SRC : .c = .o) 6 EXEC = demo 7                                                                                                                                                                                                 8 .PHONY: start 9                                                                                                                                                                                                10 start: $(OBJS)11 12         $(CC)   -o $(EXEC) $(OBJS)    -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient13 14 $(OBJS): $(SRC)15 16         $(CC)  -g  -Wall $(OBJS) -c $(SRC)  #-I/usr/include/mysql  -L/usr/lib/mysql  -lmysqlclient17  18 .PHONY: clean19 20 clean:21          rm -f $(OBJS)22  
复制代码

 

/*
几点说明,对于MYSQL数据库程序进行编译,无法像编译普通程序那样,而是要制定include路径,库文件路径和链接模块mysqlclient , 在某些系统上,可能还要用-lz选项链接压缩库。 假设MYSQL的头文件在、usr/include/mysql 路径下 ,库文件在/usr/lib/mysql 路径下,则执行如下命令 
              -I/usr/include/mysql  -L/usr/lib/mysql  -lmysqlclient  
*/
 
3.   进行make编译:     make -f demo.mk
 
二:   数据查询函数
   
[gxjun@localhost demo1]$ cat query.c
//select.c
复制代码
 1 #include<stdio.h> 2 #include"mysql.h" 3  4 int main (void) 5 { 6  7   MYSQL mysql; 8   MYSQL_RES *res; 9   MYSQL_ROW row;10   char *query;11   int flag, t;                  //connect the database12 13   mysql_init (&mysql);14   if (!mysql_real_connect15       (&mysql, "localhost", "root", "123", "demo", 0, NULL, 0))16     {17       printf ("Failed to connect to Mysql! \n");18       return 0;19     }20   else21     {22       printf ("Connected MySQL successfully!\n");23       query = "select * from student";24       flag = mysql_real_query (&mysql, query, (unsigned int) strlen (query));25       if (flag)26         {27           printf ("query failed ! \n");28           return 0;29         }30       else31         {32           printf ("[ %s ] made...\n", query);33         }34       res = mysql_store_result (&mysql);35       while (row = mysql_fetch_row (res))36         {37           for (t = 0; t < mysql_num_fields (res); t++)38             printf ("%s  ", row[t]);39           printf ("\n");40         }41       mysql_close (&mysql);42       return 0;43     }44 45   return 0;46 }
复制代码

 

 
函数关于Mysql查询语句:
(1) int      STDCALL mysql_query(MYSQL *mysql, const char *q);
(2) int      STDCALL mysql_real_query(MYSQL *mysql, const char *q,  unsigned int length);
 关于这两个函数,使用较多的为(2)式
 
//makefile文件....
 
[gxjun@localhost demo1]$ cat query.mk
复制代码
 1 .SUFFIXES: .o .c 2 CC = gcc 3 SRC = query.c 4 OBJS = $(SRC: .c = .o) 5 EXEC = Demo 6 CLS = rm 7  8 .PHONY: start 9 10 start: $(OBJS)11 12         $(CC) -o $(EXEC)   $(OBJS) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient13 14 $(OBJS): $(SRC)15 16         $(CC) -g -Wall $(OBJS) -c $(SRC)   -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient17 .PHONY: clean18 clean:19         $(CLS) -f $(OBJS)
复制代码

 

 
关于bash 语言:
         [gxjun@localhost demo1]$ cat query.sh


      

复制代码
    indent -gnu query.c          echo "make is running ....!"          make -f query.mk          echo "make is stopping ....! "
复制代码

 

使用关于nm 查看想要看的函数 ;
 
  [gxjun@localhost demo1]$ nm Demo

08049aac A __bss_start
080485d0 t call_gmon_start
08049aac b completed.1
08049a64 d __CTOR_END__
08049a60 d __CTOR_LIST__
08049984 D __data_start
08049984 W data_start
08048864 t __do_global_ctors_aux
080485f4 t __do_global_dtors_aux
08049988 D __dso_handle
08049a6c d __DTOR_END__
08049a68 d __DTOR_LIST__
08049990 A _DYNAMIC
08049aac A _edata
08048980 r __EH_FRAME_BEGIN__
08049ab0 A _end
08048888 T _fini
08049984 A __fini_array_end
08049984 A __fini_array_start
080488c0 R _fp_hw
08048630 t frame_dummy
08048980 r __FRAME_END__
08049a74 A _GLOBAL_OFFSET_TABLE_
         w __gmon_start__
080484e4 T _init
08049984 A __init_array_end
08049984 A __init_array_start
080488c4 R _IO_stdin_used
08049a70 d __JCR_END__
08049a70 d __JCR_LIST__
         w _Jv_RegisterClasses
08048830 T __libc_csu_fini
08048800 T __libc_csu_init
         U __libc_start_main@@GLIBC_2.0
0804865c T main
         U mysql_close
         U mysql_fetch_row
         U mysql_init
         U mysql_num_fields
         U mysql_real_connect
         U mysql_real_query
         U mysql_store_result
0804998c d p.0
         U printf@@GLIBC_2.0
080485ac T _start
         U strlen@@GLIBC_2.0

 
 
我们想要查询的表单:
  

mysql> select * from student
    -> ;
+------+-------+
| sno  | sname |
+------+-------+
| 1001 | jim   |
+------+-------+
1 row in set (0.05 sec)

关于MYSQL函数查询结果:
   [gxjun@localhost demo1]$ ./Demo

      Connected MySQL successfully!
      [ select * from student ] made...
       1001  jim

 

---------------------------------------------华丽丽的分割线-----------------------------------------------------

 

关于数据库的插入和查询以及连接的综合案列:

 
 
[gxjun@localhost demo2]$ cat adddata.c

复制代码
 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #include "mysql.h" 5 //#include<fcntl.h> 6  7 int 8 main (void) 9 {10 11   MYSQL mysql;                  //var mysql12   MYSQL_RES *res;               //grep_result13   MYSQL_ROW row;                //mysql_row14   int r;                        //var_tmp  r15   char *query[4];16   int flag;17   //init_mysql18   mysql_init (&mysql);19   if (!mysql_real_connect20       (&mysql, "localhost", "root", "123", "demo", 0, NULL, 0))21     {22 23       printf ("can't connect the mysql ! errInfo=[%s]", mysql_error (&mysql));24 25     }26   else27     {28       printf ("connect the mysql successfully!");29       query[0] = "insert into student(sno,sname)values(1002,'tom')";30       query[1] = "insert into student(sno,sname)values(1003,'gongxijun')";31       query[2] = "insert into student(sno,sname)values(1004,'qinshihuang')";32 //insert data to mysql33       for (r = 0; r < 3; r++)34         {35           if (mysql_real_query36               (&mysql, query[r], (unsigned int) strlen (query[r])))37             {38               printf ("insert data[%d] is failed !\n", r);39               return 0;40             }41           else42             {43               printf ("Insert data[%d] is successfully !\n", r);44             }45         }46     }47 48 //query part49   query[3] = "select * from student";50   flag =51     mysql_real_query (&mysql, query[3], (unsigned int) strlen (query[3]));52   if (!flag)53     {54       res = mysql_store_result (&mysql);55       while (row = mysql_fetch_row (res))56         {57           for (r = 0; r < mysql_num_fields (res); r++)58             {59               printf ("%s ", row[r]);60             }61           printf ("\n");62         }63     }64   else65     {66       printf ("query failed !\n");67       return 0;68 69     }70   mysql_close (&mysql);71   return 0;72 }73  
复制代码

 

//关于makefile文件:
 
 [gxjun@localhost demo2]$ cat adddata.mk
复制代码
 1 .SUFFIXES: .o .c 2 CC = gcc 3 SRC = adddata.c 4 OBJS = $(SRC: .c = .o) 5 EXEC = Demo 6  7 .PHONY: start 8 start: $(OBJS) 9 10         $(CC) -o $(EXEC) $(OBJS) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient11 12 $(OBJS): $(SRC)13 14         $(CC) -g  -Wall $(OBJS) -c $(SRC)  -I/usr/include/mysql -L/usr/lib/mysql -lmysqlcilent15 16 .PHONY: clean17 18 clean:19         rm -f $(OBJS) core.*20 21  
复制代码

 

//关于shell文件
  [gxjun@localhost demo2]$ cat adddata.sh
复制代码
1 #!/bin/bash/2 3 echo "make is starting ....!"4 indent -gnu adddata.c5 6 make -f adddata.mk7 echo "make is endding ...! "8 9  
复制代码

 

显示结果:
[gxjun@localhost demo2]$ ./Demo
connect the mysql successfully!Insert data[0] is successfully !
Insert data[1] is successfully !
Insert data[2] is successfully !
1001 jim
1002 tom
1003 gongxijun
1004 qinshihuang
[gxjun@localhost demo2]$ ls
 
 
 
关于数据库的插入和查询以及连接的综合案列:
 
 
[gxjun@localhost demo2]$ cat adddata.c

复制代码
 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #include "mysql.h" 5 //#include<fcntl.h> 6  7 int 8 main (void) 9 {10 11   MYSQL mysql;                  //var mysql12   MYSQL_RES *res;               //grep_result13   MYSQL_ROW row;                //mysql_row14   int r;                        //var_tmp  r15   char *query[4];16   int flag;17   //init_mysql18   mysql_init (&mysql);19   if (!mysql_real_connect20       (&mysql, "localhost", "root", "123", "demo", 0, NULL, 0))21     {22 23       printf ("can't connect the mysql ! errInfo=[%s]", mysql_error (&mysql));24 25     }26   else27     {28       printf ("connect the mysql successfully!");29       query[0] = "insert into student(sno,sname)values(1002,'tom')";30       query[1] = "insert into student(sno,sname)values(1003,'gongxijun')";31       query[2] = "insert into student(sno,sname)values(1004,'qinshihuang')";32 //insert data to mysql33       for (r = 0; r < 3; r++)34         {35           if (mysql_real_query36               (&mysql, query[r], (unsigned int) strlen (query[r])))37             {38               printf ("insert data[%d] is failed !\n", r);39               return 0;40             }41           else42             {43               printf ("Insert data[%d] is successfully !\n", r);44             }45         }46     }47 48 //query part49   query[3] = "select * from student";50   flag =51     mysql_real_query (&mysql, query[3], (unsigned int) strlen (query[3]));52   if (!flag)53     {54       res = mysql_store_result (&mysql);55       while (row = mysql_fetch_row (res))56         {57           for (r = 0; r < mysql_num_fields (res); r++)58             {59               printf ("%s ", row[r]);60             }61           printf ("\n");62         }63     }64   else65     {66       printf ("query failed !\n");67       return 0;68 69     }70   mysql_close (&mysql);71   return 0;72 }73  
复制代码

 

//关于makefile文件:
 
 [gxjun@localhost demo2]$ cat adddata.mk
复制代码
 1 .SUFFIXES: .o .c 2 CC = gcc 3 SRC = adddata.c 4 OBJS = $(SRC: .c = .o) 5 EXEC = Demo 6  7 .PHONY: start 8 start: $(OBJS) 9 10         $(CC) -o $(EXEC) $(OBJS) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient11 12 $(OBJS): $(SRC)13 14         $(CC) -g  -Wall $(OBJS) -c $(SRC)  -I/usr/include/mysql -L/usr/lib/mysql -lmysqlcilent15 16 .PHONY: clean17 18 clean:19         rm -f $(OBJS) core.*
复制代码

 


 
//关于shell文件
  [gxjun@localhost demo2]$ cat adddata.sh
复制代码
1 #!/bin/bash/2 3 echo "make is starting ....!"4 indent -gnu adddata.c5 6 make -f adddata.mk7 echo "make is endding ...! "8  
复制代码

 


显示结果:
[gxjun@localhost demo2]$ ./Demo
connect the mysql successfully!Insert data[0] is successfully !
Insert data[1] is successfully !
Insert data[2] is successfully !
1001 jim
1002 tom
1003 gongxijun
1004 qinshihuang
[gxjun@localhost demo2]$ ls

-------------------------------------华丽丽的分割线-----------------------------------------------------------

|++++++++++++++++++++++显示删除和查询功能++++++++++++++++++++++++++++++++++++|

 

 

del.c代码:
 
复制代码
 1 #include<stdio.h> 2 #include<string.h> 3 #include "mysql.h" 4  5 int 6 main (void) 7 { 8  9   MYSQL mysql;10 11   MYSQL_RES *res;12 13   MYSQL_ROW row;14   char *query;15   int rr, flag;16   mysql_init (&mysql);          //init mysql17   MYSQL *pts = mysql_real_connect (&mysql, "localhost", "root", "123", "demo", 0, NULL, 0);     //to connect the mysql18   if (pts == NULL)19     {20       printf ("connect is failed !  [%s ]\n", mysql_error (&mysql));21       return 0;22     }23   printf ("conected successfully ! \n");24 //the part of function is to query the data of mysql25 26   query = "select * from student ";27 28   flag = mysql_real_query (&mysql, query, (unsigned int) strlen (query));29   if (flag)30     {31       printf ("query the mysql is failed ! [%s ] \n", mysql_error (&mysql));32       return 0;33     }34 35   res = mysql_store_result (&mysql);    // return a point of res (var MYSQL_RES *)36 37   while (row = mysql_fetch_row (res))38     {                           //to next row39 40       for (rr = 0; rr < mysql_num_fields (res); rr++)41         printf ("[ %s ] ", row[rr]);42       puts ("");43     }44   //the part of del the data  of mysql45   query = "delete from student where sname = 'gongxijun'";46   flag = mysql_real_query (&mysql, query, (unsigned int) strlen (query));47   if (flag)48     {49       printf ("sorry ,delect the data of mysql is failed !\n [%s ] ",50               mysql_error (&mysql));51       return 0;52     }53   //query again54   query = "select * from student ";55   flag = mysql_real_query (&mysql, query, (unsigned int) strlen (query));56   if (flag)57     {58       printf ("query failed ! the result= [%s ] \n", mysql_error (&mysql));59       return 0;60     }61   res = mysql_store_result (&mysql);62   while (row = mysql_fetch_row (res))63     {64       for (rr = 0; rr < mysql_num_fields (res); rr++)65         printf ("[%s ]\n", row[rr]);66       printf ("\n");67     }68   mysql_close (&mysql);         //close mysql69   return 0;70 }71  
复制代码

 

关于makefile文件  :   del.mk
 
复制代码
 1   .SUFFIXES: .o .c 2 CC = gcc 3 SRC = del.c 4 OBJS = $(SRC: .c =.o) 5 EXEC = Demo 6                                                                                                                                                    7 .PHONY: start 8 start: $(OBJS) 9                                                                                                                                                   10         $(CC) -o $(EXEC) $(OBJS) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient11                                                                                                                                                   12 $(OBJS): $(SRC)13                                                                                                                                                   14         $(CC) -g -Wall $(OBJS) -c  $(SRC) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient15                                                                                                                                                   16 .PHONY: clean17 clean:18         rm -f $(OBJS) core.*19                                                                                                                                                   20 ~21 ~
复制代码

 

 
  关于bash文件:
 
indent -gnu del.c

 make -f del.mk
 显示结果:
    [gxjun@localhost demo3]$ bash del.sh
bash: /root/.bashrc: 权限不够
make: Circular del.c <- del.c dependency dropped.
gcc -o Demo del.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
[gxjun@localhost demo3]$ ./Demo
conected successfully !
[ 1001 ] [ jim ]
[ 1002 ] [ tom ]
[ 1003 ] [ gongxijun ]
[ 1004 ] [ qinshihuang ]
sorry ,delect the data of mysql is failed !
[You have an error in your SQL syntax near '* from student where sname =gongxijun' at line 1 ] [gxjun@localhost demo3]$ vi del.c
[gxjun@localhost demo3]$ bash del.sh
bash: /root/.bashrc: 权限不够
make: Circular del.c <- del.c dependency dropped.
gcc -o Demo del.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
[gxjun@localhost demo3]$ ./Demo
conected successfully !
[ 1001 ] [ jim ]
[ 1002 ] [ tom ]
[ 1003 ] [ gongxijun ]
[ 1004 ] [ qinshihuang ]
sorry ,delect the data of mysql is failed !
[Unknown column 'gongxijun' in 'where clause' ] [gxjun@localhost demo3]$
[gxjun@localhost demo3]$
[gxjun@localhost demo3]$ vi del.c
[gxjun@localhost demo3]$ bash del.sh
bash: /root/.bashrc: 权限不够
make: Circular del.c <- del.c dependency dropped.
gcc -o Demo del.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
[gxjun@localhost demo3]$ ./Demo
conected successfully !
[ 1001 ] [ jim ]
[ 1002 ] [ tom ]
[ 1003 ] [ gongxijun ]
[ 1004 ] [ qinshihuang ]
[1001 ]
[jim ]

[1002 ]
[tom ]

[1004 ]
[qinshihuang ]

                                                                                                                                                  

 

编程是一种快乐,享受代码带给我的乐趣!!!

linux下mysql函数的详细案列

1 MYSQL  *    STDCALL mysql_real_connect(MYSQL *mysql, const char *host,2                                            const char *user,3                                            const char *passwd,4                                            unsigned int port,5                                            const char *unix_socket,6                                            unsigned int clientflag);

 

 
 
1.  如何连接数据mysql数据库...。
 
[gxjun@localhost Mysql]$ cat demo.c

                                                                                                                                                                                                

复制代码
 1 //connect to mysql 2 #include<stdio.h> 3 #include"mysql.h" 4                                                                                                                                                                                                  5 int main (void) 6 {                                                                                                                                                                                                7   MYSQL mysql;                  //need a instance to init 8                                                                                                                                                                                                  9   int t, r;                     //connect the database10                                                                                                                                                                                                 11   mysql_init (&mysql);12                                                                                                                                                                                                 13   if (!mysql_real_connect14       (&mysql, "localhost", "root", "123", "demo", 0, NULL, 0))15     {16       printf ("Error connecting to database:%s\n", mysql_error (&mysql));17     }18   else19     {20       printf ("Connected MYSQL successfully!\n");21     }22   mysql_close (&mysql);23   return 0;24 }25 26  
复制代码

 

2.关于make文件的内容:
 
[gxjun@localhost Mysql]$ cat demo.mk
复制代码
 1 .SUFFIXES: .o .c 2                                                                                                                                                                                                 3 CC = gcc 4 SRC = demo.c 5 OBJS = $(SRC : .c = .o) 6 EXEC = demo 7                                                                                                                                                                                                 8 .PHONY: start 9                                                                                                                                                                                                10 start: $(OBJS)11 12         $(CC)   -o $(EXEC) $(OBJS)    -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient13 14 $(OBJS): $(SRC)15 16         $(CC)  -g  -Wall $(OBJS) -c $(SRC)  #-I/usr/include/mysql  -L/usr/lib/mysql  -lmysqlclient17  18 .PHONY: clean19 20 clean:21          rm -f $(OBJS)22  
复制代码

 

/*
几点说明,对于MYSQL数据库程序进行编译,无法像编译普通程序那样,而是要制定include路径,库文件路径和链接模块mysqlclient , 在某些系统上,可能还要用-lz选项链接压缩库。 假设MYSQL的头文件在、usr/include/mysql 路径下 ,库文件在/usr/lib/mysql 路径下,则执行如下命令 
              -I/usr/include/mysql  -L/usr/lib/mysql  -lmysqlclient  
*/
 
3.   进行make编译:     make -f demo.mk
 
二:   数据查询函数
   
[gxjun@localhost demo1]$ cat query.c
//select.c
复制代码
 1 #include<stdio.h> 2 #include"mysql.h" 3  4 int main (void) 5 { 6  7   MYSQL mysql; 8   MYSQL_RES *res; 9   MYSQL_ROW row;10   char *query;11   int flag, t;                  //connect the database12 13   mysql_init (&mysql);14   if (!mysql_real_connect15       (&mysql, "localhost", "root", "123", "demo", 0, NULL, 0))16     {17       printf ("Failed to connect to Mysql! \n");18       return 0;19     }20   else21     {22       printf ("Connected MySQL successfully!\n");23       query = "select * from student";24       flag = mysql_real_query (&mysql, query, (unsigned int) strlen (query));25       if (flag)26         {27           printf ("query failed ! \n");28           return 0;29         }30       else31         {32           printf ("[ %s ] made...\n", query);33         }34       res = mysql_store_result (&mysql);35       while (row = mysql_fetch_row (res))36         {37           for (t = 0; t < mysql_num_fields (res); t++)38             printf ("%s  ", row[t]);39           printf ("\n");40         }41       mysql_close (&mysql);42       return 0;43     }44 45   return 0;46 }
复制代码

 

 
函数关于Mysql查询语句:
(1) int      STDCALL mysql_query(MYSQL *mysql, const char *q);
(2) int      STDCALL mysql_real_query(MYSQL *mysql, const char *q,  unsigned int length);
 关于这两个函数,使用较多的为(2)式
 
//makefile文件....
 
[gxjun@localhost demo1]$ cat query.mk
复制代码
 1 .SUFFIXES: .o .c 2 CC = gcc 3 SRC = query.c 4 OBJS = $(SRC: .c = .o) 5 EXEC = Demo 6 CLS = rm 7  8 .PHONY: start 9 10 start: $(OBJS)11 12         $(CC) -o $(EXEC)   $(OBJS) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient13 14 $(OBJS): $(SRC)15 16         $(CC) -g -Wall $(OBJS) -c $(SRC)   -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient17 .PHONY: clean18 clean:19         $(CLS) -f $(OBJS)
复制代码

 

 
关于bash 语言:
         [gxjun@localhost demo1]$ cat query.sh


      

复制代码
    indent -gnu query.c          echo "make is running ....!"          make -f query.mk          echo "make is stopping ....! "
复制代码

 

使用关于nm 查看想要看的函数 ;
 
  [gxjun@localhost demo1]$ nm Demo

08049aac A __bss_start
080485d0 t call_gmon_start
08049aac b completed.1
08049a64 d __CTOR_END__
08049a60 d __CTOR_LIST__
08049984 D __data_start
08049984 W data_start
08048864 t __do_global_ctors_aux
080485f4 t __do_global_dtors_aux
08049988 D __dso_handle
08049a6c d __DTOR_END__
08049a68 d __DTOR_LIST__
08049990 A _DYNAMIC
08049aac A _edata
08048980 r __EH_FRAME_BEGIN__
08049ab0 A _end
08048888 T _fini
08049984 A __fini_array_end
08049984 A __fini_array_start
080488c0 R _fp_hw
08048630 t frame_dummy
08048980 r __FRAME_END__
08049a74 A _GLOBAL_OFFSET_TABLE_
         w __gmon_start__
080484e4 T _init
08049984 A __init_array_end
08049984 A __init_array_start
080488c4 R _IO_stdin_used
08049a70 d __JCR_END__
08049a70 d __JCR_LIST__
         w _Jv_RegisterClasses
08048830 T __libc_csu_fini
08048800 T __libc_csu_init
         U __libc_start_main@@GLIBC_2.0
0804865c T main
         U mysql_close
         U mysql_fetch_row
         U mysql_init
         U mysql_num_fields
         U mysql_real_connect
         U mysql_real_query
         U mysql_store_result
0804998c d p.0
         U printf@@GLIBC_2.0
080485ac T _start
         U strlen@@GLIBC_2.0

 
 
我们想要查询的表单:
  

mysql> select * from student
    -> ;
+------+-------+
| sno  | sname |
+------+-------+
| 1001 | jim   |
+------+-------+
1 row in set (0.05 sec)

关于MYSQL函数查询结果:
   [gxjun@localhost demo1]$ ./Demo

      Connected MySQL successfully!
      [ select * from student ] made...
       1001  jim

 

---------------------------------------------华丽丽的分割线-----------------------------------------------------

 

关于数据库的插入和查询以及连接的综合案列:

 
 
[gxjun@localhost demo2]$ cat adddata.c

复制代码
 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #include "mysql.h" 5 //#include<fcntl.h> 6  7 int 8 main (void) 9 {10 11   MYSQL mysql;                  //var mysql12   MYSQL_RES *res;               //grep_result13   MYSQL_ROW row;                //mysql_row14   int r;                        //var_tmp  r15   char *query[4];16   int flag;17   //init_mysql18   mysql_init (&mysql);19   if (!mysql_real_connect20       (&mysql, "localhost", "root", "123", "demo", 0, NULL, 0))21     {22 23       printf ("can't connect the mysql ! errInfo=[%s]", mysql_error (&mysql));24 25     }26   else27     {28       printf ("connect the mysql successfully!");29       query[0] = "insert into student(sno,sname)values(1002,'tom')";30       query[1] = "insert into student(sno,sname)values(1003,'gongxijun')";31       query[2] = "insert into student(sno,sname)values(1004,'qinshihuang')";32 //insert data to mysql33       for (r = 0; r < 3; r++)34         {35           if (mysql_real_query36               (&mysql, query[r], (unsigned int) strlen (query[r])))37             {38               printf ("insert data[%d] is failed !\n", r);39               return 0;40             }41           else42             {43               printf ("Insert data[%d] is successfully !\n", r);44             }45         }46     }47 48 //query part49   query[3] = "select * from student";50   flag =51     mysql_real_query (&mysql, query[3], (unsigned int) strlen (query[3]));52   if (!flag)53     {54       res = mysql_store_result (&mysql);55       while (row = mysql_fetch_row (res))56         {57           for (r = 0; r < mysql_num_fields (res); r++)58             {59               printf ("%s ", row[r]);60             }61           printf ("\n");62         }63     }64   else65     {66       printf ("query failed !\n");67       return 0;68 69     }70   mysql_close (&mysql);71   return 0;72 }73  
复制代码

 

//关于makefile文件:
 
 [gxjun@localhost demo2]$ cat adddata.mk
复制代码
 1 .SUFFIXES: .o .c 2 CC = gcc 3 SRC = adddata.c 4 OBJS = $(SRC: .c = .o) 5 EXEC = Demo 6  7 .PHONY: start 8 start: $(OBJS) 9 10         $(CC) -o $(EXEC) $(OBJS) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient11 12 $(OBJS): $(SRC)13 14         $(CC) -g  -Wall $(OBJS) -c $(SRC)  -I/usr/include/mysql -L/usr/lib/mysql -lmysqlcilent15 16 .PHONY: clean17 18 clean:19         rm -f $(OBJS) core.*20 21  
复制代码

 

//关于shell文件
  [gxjun@localhost demo2]$ cat adddata.sh
复制代码
1 #!/bin/bash/2 3 echo "make is starting ....!"4 indent -gnu adddata.c5 6 make -f adddata.mk7 echo "make is endding ...! "8 9  
复制代码

 

显示结果:
[gxjun@localhost demo2]$ ./Demo
connect the mysql successfully!Insert data[0] is successfully !
Insert data[1] is successfully !
Insert data[2] is successfully !
1001 jim
1002 tom
1003 gongxijun
1004 qinshihuang
[gxjun@localhost demo2]$ ls
 
 
 
关于数据库的插入和查询以及连接的综合案列:
 
 
[gxjun@localhost demo2]$ cat adddata.c

复制代码
 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #include "mysql.h" 5 //#include<fcntl.h> 6  7 int 8 main (void) 9 {10 11   MYSQL mysql;                  //var mysql12   MYSQL_RES *res;               //grep_result13   MYSQL_ROW row;                //mysql_row14   int r;                        //var_tmp  r15   char *query[4];16   int flag;17   //init_mysql18   mysql_init (&mysql);19   if (!mysql_real_connect20       (&mysql, "localhost", "root", "123", "demo", 0, NULL, 0))21     {22 23       printf ("can't connect the mysql ! errInfo=[%s]", mysql_error (&mysql));24 25     }26   else27     {28       printf ("connect the mysql successfully!");29       query[0] = "insert into student(sno,sname)values(1002,'tom')";30       query[1] = "insert into student(sno,sname)values(1003,'gongxijun')";31       query[2] = "insert into student(sno,sname)values(1004,'qinshihuang')";32 //insert data to mysql33       for (r = 0; r < 3; r++)34         {35           if (mysql_real_query36               (&mysql, query[r], (unsigned int) strlen (query[r])))37             {38               printf ("insert data[%d] is failed !\n", r);39               return 0;40             }41           else42             {43               printf ("Insert data[%d] is successfully !\n", r);44             }45         }46     }47 48 //query part49   query[3] = "select * from student";50   flag =51     mysql_real_query (&mysql, query[3], (unsigned int) strlen (query[3]));52   if (!flag)53     {54       res = mysql_store_result (&mysql);55       while (row = mysql_fetch_row (res))56         {57           for (r = 0; r < mysql_num_fields (res); r++)58             {59               printf ("%s ", row[r]);60             }61           printf ("\n");62         }63     }64   else65     {66       printf ("query failed !\n");67       return 0;68 69     }70   mysql_close (&mysql);71   return 0;72 }73  
复制代码

 

//关于makefile文件:
 
 [gxjun@localhost demo2]$ cat adddata.mk
复制代码
 1 .SUFFIXES: .o .c 2 CC = gcc 3 SRC = adddata.c 4 OBJS = $(SRC: .c = .o) 5 EXEC = Demo 6  7 .PHONY: start 8 start: $(OBJS) 9 10         $(CC) -o $(EXEC) $(OBJS) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient11 12 $(OBJS): $(SRC)13 14         $(CC) -g  -Wall $(OBJS) -c $(SRC)  -I/usr/include/mysql -L/usr/lib/mysql -lmysqlcilent15 16 .PHONY: clean17 18 clean:19         rm -f $(OBJS) core.*
复制代码

 


 
//关于shell文件
  [gxjun@localhost demo2]$ cat adddata.sh
复制代码
1 #!/bin/bash/2 3 echo "make is starting ....!"4 indent -gnu adddata.c5 6 make -f adddata.mk7 echo "make is endding ...! "8  
复制代码

 


显示结果:
[gxjun@localhost demo2]$ ./Demo
connect the mysql successfully!Insert data[0] is successfully !
Insert data[1] is successfully !
Insert data[2] is successfully !
1001 jim
1002 tom
1003 gongxijun
1004 qinshihuang
[gxjun@localhost demo2]$ ls

-------------------------------------华丽丽的分割线-----------------------------------------------------------

|++++++++++++++++++++++显示删除和查询功能++++++++++++++++++++++++++++++++++++|

 

 

del.c代码:
 
复制代码
 1 #include<stdio.h> 2 #include<string.h> 3 #include "mysql.h" 4  5 int 6 main (void) 7 { 8  9   MYSQL mysql;10 11   MYSQL_RES *res;12 13   MYSQL_ROW row;14   char *query;15   int rr, flag;16   mysql_init (&mysql);          //init mysql17   MYSQL *pts = mysql_real_connect (&mysql, "localhost", "root", "123", "demo", 0, NULL, 0);     //to connect the mysql18   if (pts == NULL)19     {20       printf ("connect is failed !  [%s ]\n", mysql_error (&mysql));21       return 0;22     }23   printf ("conected successfully ! \n");24 //the part of function is to query the data of mysql25 26   query = "select * from student ";27 28   flag = mysql_real_query (&mysql, query, (unsigned int) strlen (query));29   if (flag)30     {31       printf ("query the mysql is failed ! [%s ] \n", mysql_error (&mysql));32       return 0;33     }34 35   res = mysql_store_result (&mysql);    // return a point of res (var MYSQL_RES *)36 37   while (row = mysql_fetch_row (res))38     {                           //to next row39 40       for (rr = 0; rr < mysql_num_fields (res); rr++)41         printf ("[ %s ] ", row[rr]);42       puts ("");43     }44   //the part of del the data  of mysql45   query = "delete from student where sname = 'gongxijun'";46   flag = mysql_real_query (&mysql, query, (unsigned int) strlen (query));47   if (flag)48     {49       printf ("sorry ,delect the data of mysql is failed !\n [%s ] ",50               mysql_error (&mysql));51       return 0;52     }53   //query again54   query = "select * from student ";55   flag = mysql_real_query (&mysql, query, (unsigned int) strlen (query));56   if (flag)57     {58       printf ("query failed ! the result= [%s ] \n", mysql_error (&mysql));59       return 0;60     }61   res = mysql_store_result (&mysql);62   while (row = mysql_fetch_row (res))63     {64       for (rr = 0; rr < mysql_num_fields (res); rr++)65         printf ("[%s ]\n", row[rr]);66       printf ("\n");67     }68   mysql_close (&mysql);         //close mysql69   return 0;70 }71  
复制代码

 

关于makefile文件  :   del.mk
 
复制代码
 1   .SUFFIXES: .o .c 2 CC = gcc 3 SRC = del.c 4 OBJS = $(SRC: .c =.o) 5 EXEC = Demo 6                                                                                                                                                    7 .PHONY: start 8 start: $(OBJS) 9                                                                                                                                                   10         $(CC) -o $(EXEC) $(OBJS) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient11                                                                                                                                                   12 $(OBJS): $(SRC)13                                                                                                                                                   14         $(CC) -g -Wall $(OBJS) -c  $(SRC) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient15                                                                                                                                                   16 .PHONY: clean17 clean:18         rm -f $(OBJS) core.*19                                                                                                                                                   20 ~21 ~
复制代码

 

 
  关于bash文件:
 
indent -gnu del.c

 make -f del.mk
 显示结果:
    [gxjun@localhost demo3]$ bash del.sh
bash: /root/.bashrc: 权限不够
make: Circular del.c <- del.c dependency dropped.
gcc -o Demo del.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
[gxjun@localhost demo3]$ ./Demo
conected successfully !
[ 1001 ] [ jim ]
[ 1002 ] [ tom ]
[ 1003 ] [ gongxijun ]
[ 1004 ] [ qinshihuang ]
sorry ,delect the data of mysql is failed !
[You have an error in your SQL syntax near '* from student where sname =gongxijun' at line 1 ] [gxjun@localhost demo3]$ vi del.c
[gxjun@localhost demo3]$ bash del.sh
bash: /root/.bashrc: 权限不够
make: Circular del.c <- del.c dependency dropped.
gcc -o Demo del.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
[gxjun@localhost demo3]$ ./Demo
conected successfully !
[ 1001 ] [ jim ]
[ 1002 ] [ tom ]
[ 1003 ] [ gongxijun ]
[ 1004 ] [ qinshihuang ]
sorry ,delect the data of mysql is failed !
[Unknown column 'gongxijun' in 'where clause' ] [gxjun@localhost demo3]$
[gxjun@localhost demo3]$
[gxjun@localhost demo3]$ vi del.c
[gxjun@localhost demo3]$ bash del.sh
bash: /root/.bashrc: 权限不够
make: Circular del.c <- del.c dependency dropped.
gcc -o Demo del.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
[gxjun@localhost demo3]$ ./Demo
conected successfully !
[ 1001 ] [ jim ]
[ 1002 ] [ tom ]
[ 1003 ] [ gongxijun ]
[ 1004 ] [ qinshihuang ]
[1001 ]
[jim ]

[1002 ]
[tom ]

[1004 ]
[qinshihuang ]

                                                                                                                                                  

 

编程是一种快乐,享受代码带给我的乐趣!!!
原创粉丝点击