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 //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
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
//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 }
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)
indent -gnu query.c echo "make is running ....!" make -f query.mk echo "make is stopping ....! "
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)
Connected MySQL successfully!
[ select * from student ] made...
1001 jim
---------------------------------------------华丽丽的分割线-----------------------------------------------------
关于数据库的插入和查询以及连接的综合案列:
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
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
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
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
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
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.*
1 #!/bin/bash/2 3 echo "make is starting ....!"4 indent -gnu adddata.c5 6 make -f adddata.mk7 echo "make is endding ...! "8
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
-------------------------------------华丽丽的分割线-----------------------------------------------------------
|++++++++++++++++++++++显示删除和查询功能++++++++++++++++++++++++++++++++++++|
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
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 ~
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 ]
Gxjun
修炼
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 //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
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
//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 }
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)
indent -gnu query.c echo "make is running ....!" make -f query.mk echo "make is stopping ....! "
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)
Connected MySQL successfully!
[ select * from student ] made...
1001 jim
---------------------------------------------华丽丽的分割线-----------------------------------------------------
关于数据库的插入和查询以及连接的综合案列:
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
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
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
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
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
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.*
1 #!/bin/bash/2 3 echo "make is starting ....!"4 indent -gnu adddata.c5 6 make -f adddata.mk7 echo "make is endding ...! "8
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
-------------------------------------华丽丽的分割线-----------------------------------------------------------
|++++++++++++++++++++++显示删除和查询功能++++++++++++++++++++++++++++++++++++|
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
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 ~
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应用
- linux下mysql应用
- linux下mysql的简单应用
- MySQL安装与应用【Linux下的安装与配置】
- Linux下Apache与MySQL+PHP的综合应用案例
- linux 下的mysql
- linux下的mysql
- linux下的mysql
- linux 下的mysql
- linux下的mysql
- linux 下的mysql
- linux下的lnmp(CentOS linux+nginx+mysql+php+zend + eAccelerator)的应用介绍
- linux下的lamp(CentOS linux+apache+mysql+php+zend + eAccelerator)的应用介绍
- linux下shell的应用
- Linux下Tar的应用
- linux 下的gcc应用
- Linux下3G的应用
- Linux下rdesktop的应用
- 如何稳健股票投资
- (连载)一个很长的梦——序
- ubuntu 图形界面进不去怎么办?
- JUC-001-volatile与内存可见性
- 拦截器的请求头
- Linux下的mysql应用
- C语言基础练习16
- ListView点击checkbox其他checkbox也被同时选中的问题
- 《kubernetes-1.8.0》04-master搭建
- 【NOIP2017提高组正式赛】宝藏
- spring框架学习(一)
- 【Scikit-Learn 中文文档】支持向量机
- web笔记
- ubuntu16.04操作系统下的edk2安装