linux mysql 查看函数_linux下mysql函数的详细案列

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

1.  如何连接数据mysql数据库...。

[gxjun@localhost Mysql]$ cat demo.c

1 //connect to mysql

2 #include

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_connect14 (&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文件的内容:

[gxjun@localhost Mysql]$ cat demo.mk

1 .SUFFIXES: .o .c2

3 CC =gcc4 SRC =demo.c5 OBJS = $(SRC : .c =.o)6 EXEC =demo7

8 .PHONY: start9

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

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_connect15 (&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文件....

[gxjun@localhost demo1]$ cat query.mk

1 .SUFFIXES: .o .c2 CC =gcc3 SRC =query.c4 OBJS = $(SRC: .c =.o)5 EXEC =Demo6 CLS =rm7

8 .PHONY: start9

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.cecho "make is running ....!"

make -f query.mkecho "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

2 #include

3 #include

4 #include "mysql.h"

5 //#include

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 intflag;17 //init_mysql

18 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 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_query36 (&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文件:

[gxjun@localhost demo2]$ cat adddata.mk

1 .SUFFIXES: .o .c2 CC =gcc3 SRC =adddata.c4 OBJS = $(SRC: .c =.o)5 EXEC =Demo6

7 .PHONY: start8 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

[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

2 #include

3 #include

4 #include "mysql.h"

5 //#include

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 intflag;17 //init_mysql

18 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 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_query36 (&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文件:

[gxjun@localhost demo2]$ cat adddata.mk

1 .SUFFIXES: .o .c2 CC =gcc3 SRC =adddata.c4 OBJS = $(SRC: .c =.o)5 EXEC =Demo6

7 .PHONY: start8 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.*

[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

2 #include

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 intrr, 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 .c2 CC =gcc3 SRC =del.c4 OBJS = $(SRC: .c =.o)5 EXEC =Demo6

7 .PHONY: start8 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

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

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

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 ]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值