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数据库...。

[[email protected] 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 database
10
11   mysql_init (&mysql);
12
13   if (!mysql_real_connect
14       (&mysql, "localhost", "root", "123", "demo", 0, NULL, 0))
15     {
16       printf ("Error connecting to database:%s\n", mysql_error (&mysql));
17     }
18   else
19     {
20       printf ("Connected MYSQL successfully!\n");
21     }
22   mysql_close (&mysql);
23   return 0;
24 }
25
26  

2.关于make文件的内容:

[[email protected] 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 -lmysqlclient
13
14 $(OBJS): $(SRC)
15
16         $(CC)  -g  -Wall $(OBJS) -c $(SRC)  #-I/usr/include/mysql  -L/usr/lib/mysql  -lmysqlclient
17
18 .PHONY: clean
19
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

二:   数据查询函数

[[email protected] 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 database
12
13   mysql_init (&mysql);
14   if (!mysql_real_connect
15       (&mysql, "localhost", "root", "123", "demo", 0, NULL, 0))
16     {
17       printf ("Failed to connect to Mysql! \n");
18       return 0;
19     }
20   else
21     {
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       else
31         {
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文件....

[[email protected] 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 -lmysqlclient
13
14 $(OBJS): $(SRC)
15
16         $(CC) -g -Wall $(OBJS) -c $(SRC)   -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
17 .PHONY: clean
18 clean:
19         $(CLS) -f $(OBJS)

关于bash 语言:

[[email protected] demo1]$ cat query.sh

    indent -gnu query.c

          echo "make is running ....!"

          make -f query.mk

          echo "make is stopping ....! "

使用关于nm 查看想要看的函数 ;

[[email protected] 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 [email protected]@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 [email protected]@GLIBC_2.0
080485ac T _start
         U [email protected]@GLIBC_2.0

我们想要查询的表单:

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

关于MYSQL函数查询结果:

[[email protected] demo1]$ ./Demo

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

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

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

[[email protected] 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 mysql
12   MYSQL_RES *res;               //grep_result
13   MYSQL_ROW row;                //mysql_row
14   int r;                        //var_tmp  r
15   char *query[4];
16   int flag;
17   //init_mysql
18   mysql_init (&mysql);
19   if (!mysql_real_connect
20       (&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   else
27     {
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 mysql
33       for (r = 0; r < 3; r++)
34         {
35           if (mysql_real_query
36               (&mysql, query[r], (unsigned int) strlen (query[r])))
37             {
38               printf ("insert data[%d] is failed !\n", r);
39               return 0;
40             }
41           else
42             {
43               printf ("Insert data[%d] is successfully !\n", r);
44             }
45         }
46     }
47
48 //query part
49   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   else
65     {
66       printf ("query failed !\n");
67       return 0;
68
69     }
70   mysql_close (&mysql);
71   return 0;
72 }
73  

//关于makefile文件:

[[email protected] 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 -lmysqlclient
11
12 $(OBJS): $(SRC)
13
14         $(CC) -g  -Wall $(OBJS) -c $(SRC)  -I/usr/include/mysql -L/usr/lib/mysql -lmysqlcilent
15
16 .PHONY: clean
17
18 clean:
19         rm -f $(OBJS) core.*
20
21  

//关于shell文件

[[email protected] demo2]$ cat adddata.sh

1 #!/bin/bash/
2
3 echo "make is starting ....!"
4 indent -gnu adddata.c
5
6 make -f adddata.mk
7 echo "make is endding ...! "
8
9  

显示结果:

[[email protected] 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
[[email protected] demo2]$ ls

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

[[email protected] 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 mysql
12   MYSQL_RES *res;               //grep_result
13   MYSQL_ROW row;                //mysql_row
14   int r;                        //var_tmp  r
15   char *query[4];
16   int flag;
17   //init_mysql
18   mysql_init (&mysql);
19   if (!mysql_real_connect
20       (&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   else
27     {
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 mysql
33       for (r = 0; r < 3; r++)
34         {
35           if (mysql_real_query
36               (&mysql, query[r], (unsigned int) strlen (query[r])))
37             {
38               printf ("insert data[%d] is failed !\n", r);
39               return 0;
40             }
41           else
42             {
43               printf ("Insert data[%d] is successfully !\n", r);
44             }
45         }
46     }
47
48 //query part
49   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   else
65     {
66       printf ("query failed !\n");
67       return 0;
68
69     }
70   mysql_close (&mysql);
71   return 0;
72 }
73  

//关于makefile文件:

[[email protected] 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 -lmysqlclient
11
12 $(OBJS): $(SRC)
13
14         $(CC) -g  -Wall $(OBJS) -c $(SRC)  -I/usr/include/mysql -L/usr/lib/mysql -lmysqlcilent
15
16 .PHONY: clean
17
18 clean:
19         rm -f $(OBJS) core.*

//关于shell文件

[[email protected] demo2]$ cat adddata.sh

1 #!/bin/bash/
2
3 echo "make is starting ....!"
4 indent -gnu adddata.c
5
6 make -f adddata.mk
7 echo "make is endding ...! "
8  

显示结果:

[[email protected] 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
[[email protected] 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 mysql
17   MYSQL *pts = mysql_real_connect (&mysql, "localhost", "root", "123", "demo", 0, NULL, 0);     //to connect the mysql
18   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 mysql
25
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 row
39
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 mysql
45   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 again
54   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 mysql
69   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 -lmysqlclient
11
12 $(OBJS): $(SRC)
13
14         $(CC) -g -Wall $(OBJS) -c  $(SRC) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
15
16 .PHONY: clean
17 clean:
18         rm -f $(OBJS) core.*
19
20 ~
21 ~

关于bash文件:

indent -gnu del.c

make -f del.mk
 显示结果:
    [[email protected] 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
[[email protected] 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 ] [[email protected] demo3]$ vi del.c
[[email protected] 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
[[email protected] 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‘ ] [[email protected] demo3]$
[[email protected] demo3]$
[[email protected] demo3]$ vi del.c
[[email protected] 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
[[email protected] demo3]$ ./Demo
conected successfully !
[ 1001 ] [ jim ]
[ 1002 ] [ tom ]
[ 1003 ] [ gongxijun ]
[ 1004 ] [ qinshihuang ]
[1001 ]
[jim ]

[1002 ]
[tom ]

[1004 ]
[qinshihuang ]

时间: 2024-10-10 17:48:00

linux下mysql函数的详细案列的相关文章

linux下mysql主从配置详细教程

1.修改MySQL配置:主库配置server-id = 3binlog-do-db=xmcp_gxfc #the db need to syncbinlog-ignore-db = mysql #不需要同步的数据库binlog-ignore-db = redmine #不需要同步的数据库log_slave_updates = 1binlog_format=mixedrelay_log = /usr/local/mysql/relay_log/mysql-relay-binread_only =

linux下MySQL安装登录及操作

linux下MySQL安装登录及操作 二.安装Mysql 1.下载MySQL的安装文件 安装MySQL需要下面两个文件: MySQL-server-4.0.16-0.i386.rpm MySQL-client-4.0.16-0.i386.rpm 下载地址为:http://www.mysql.com/downloads/mysql-4.0.html, 打开此网页,下拉网页找到“Linux x86 RPM downloads”项,找到“Server”和“Client programs”项,下载需要的

对于linux下system()函数的深度理解(整理)

对于linux下system()函数的深度理解(整理) (2013-02-07 08:58:54) 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为是其他的代码影响到这个,或是内核驱动文件系统什么的异常导致,昨天有出现了这个问题,就随手百了一下度,问题出现了,很多人都说system()函数要慎用要少用要能不用则不用,system()函数不稳定?

linux下mysql 安装

小菜鸟接触linux太晚, 装个mysql(免安装 mysql-5.6.22-linux-glibc2.5-x86_64版本,最简单的安装方法) 竞折腾了两个晚上… 网上到处有linux下mysql的安装,但我自己安装过程中总出现这样那样的问题,现将此次安装过程及错误记录,以供自己日后参考,也希望可以给后来人一些帮助… 1. 去Oracle下载mysql-5.6.22-linux-glibc*.tar.gz 2.解压 tar -zxvf mysql-5.6.22-linux-glibc*.tar

【C/C++】Linux下system()函数引发的错误

http://my.oschina.net/renhc/blog/54582 [C/C++]Linux下system()函数引发的错误 恋恋美食  恋恋美食 发布时间: 2012/04/21 11:33 阅读: 11393 收藏: 21 点赞: 8 评论: 4 今天,一个运行了近一年的程序突然挂掉了,问题定位到是system()函数出的问题,关于该函数的简单使用在我上篇文章做过介绍: http://my.oschina.net/renhc/blog/53580 先看一下问题 简单封装了一下sys

linux 下mysql的启动 、调试、排错

Linux 下 MySQL 启动与关闭 说明 一.启动 1.1  MySQL 进程 可以用ps 命令查看进程: [[email protected] ~]# ps -ef|grep mysql root     2161     1  0 09:38 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe--datadir=/var/lib/mysql --pid-file=/var/lib/mysql/rac2.pid mysql    2418  216

Linux下select函数的使用

Linux下select函数的使用 转载:http://www.cnblogs.com/hjslovewcl/archive/2011/03/16/2314330.html 一.Select 函数详细介绍 Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如connect. accept.recv或recvfrom这样的阻塞程序(所谓阻塞方式block,顾名思义,就是进程或是线程执行到这些函数时必须等待某个事件的发 生,

linux下mysql表名大小写问题

近日,新mysql实例导入sql数据时,发现比老的mysql多了100+张表,最终发现是mysql表名大小写所致:很简单的问题却耽误很长时间,在此记录一下,以防再犯同样的错误: 1.Linux下mysql安装完后是默认:区分表名的大小写,不区分列名的大小写:2.用root帐号登录后,在/etc/my.cnf中的[mysqld]后添加添加lower_case_table_names=1,重启MYSQL服务,这时已设置成功:不区分表名的大小写:lower_case_table_names参数详解:l

设置Linux下Mysql表名不区分大小写

1.Linux下mysql安装完后是默认:区分表名的大小写,不区分列名的大小写:2.用root帐号登录后,在/etc/my.cnf中的[mysqld]后添加添加lower_case_table_names=1,重启MYSQL服务,这时已设置成功:不区分表名的大小写:lower_case_table_names参数详解:lower_case_table_names=0其中0:区分大小写,1:不区分大小写 MySQL在Linux下数据库名.表名.列名.别名大小写规则是这样的:1.数据库名与表名是严格