MYSQL * STDCALL mysql_real_connect(MYSQL *mysql, const char *host,
const char *user,
const char *passwd,
unsigned int port,
const char *unix_socket,
unsigned int clientflag);
//connect to mysql
#include<stdio.h>
#include"mysql.h" int main (void)
{
MYSQL mysql; //need a instance to init int t, r; //connect the database mysql_init (&mysql); if (!mysql_real_connect
(&mysql, "localhost", "root", "", "demo", , NULL, ))
{
printf ("Error connecting to database:%s\n", mysql_error (&mysql));
}
else
{
printf ("Connected MYSQL successfully!\n");
}
mysql_close (&mysql);
return ;
}
.SUFFIXES: .o .c CC = gcc
SRC = demo.c
OBJS = $(SRC : .c = .o)
EXEC = demo .PHONY: start start: $(OBJS) $(CC) -o $(EXEC) $(OBJS) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient $(OBJS): $(SRC) $(CC) -g -Wall $(OBJS) -c $(SRC) #-I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient .PHONY: clean clean:
rm -f $(OBJS)
//select.c
#include<stdio.h>
#include"mysql.h" int main (void)
{ MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
char *query;
int flag, t; //connect the database mysql_init (&mysql);
if (!mysql_real_connect
(&mysql, "localhost", "root", "", "demo", , NULL, ))
{
printf ("Failed to connect to Mysql! \n");
return ;
}
else
{
printf ("Connected MySQL successfully!\n");
query = "select * from student";
flag = mysql_real_query (&mysql, query, (unsigned int) strlen (query));
if (flag)
{
printf ("query failed ! \n");
return ;
}
else
{
printf ("[ %s ] made...\n", query);
}
res = mysql_store_result (&mysql);
while (row = mysql_fetch_row (res))
{
for (t = ; t < mysql_num_fields (res); t++)
printf ("%s ", row[t]);
printf ("\n");
}
mysql_close (&mysql);
return ;
} return ;
}
.SUFFIXES: .o .c
CC = gcc
SRC = query.c
OBJS = $(SRC: .c = .o)
EXEC = Demo
CLS = rm .PHONY: start start: $(OBJS) $(CC) -o $(EXEC) $(OBJS) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient $(OBJS): $(SRC) $(CC) -g -Wall $(OBJS) -c $(SRC) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
.PHONY: clean
clean:
$(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
---------------------------------------------华丽丽的分割线-----------------------------------------------------
关于数据库的插入和查询以及连接的综合案列:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "mysql.h"
//#include<fcntl.h> int
main (void)
{ MYSQL mysql; //var mysql
MYSQL_RES *res; //grep_result
MYSQL_ROW row; //mysql_row
int r; //var_tmp r
char *query[];
int flag;
//init_mysql
mysql_init (&mysql);
if (!mysql_real_connect
(&mysql, "localhost", "root", "", "demo", , NULL, ))
{ printf ("can't connect the mysql ! errInfo=[%s]", mysql_error (&mysql)); }
else
{
printf ("connect the mysql successfully!");
query[] = "insert into student(sno,sname)values(1002,'tom')";
query[] = "insert into student(sno,sname)values(1003,'gongxijun')";
query[] = "insert into student(sno,sname)values(1004,'qinshihuang')";
//insert data to mysql
for (r = ; r < ; r++)
{
if (mysql_real_query
(&mysql, query[r], (unsigned int) strlen (query[r])))
{
printf ("insert data[%d] is failed !\n", r);
return ;
}
else
{
printf ("Insert data[%d] is successfully !\n", r);
}
}
} //query part
query[] = "select * from student";
flag =
mysql_real_query (&mysql, query[], (unsigned int) strlen (query[]));
if (!flag)
{
res = mysql_store_result (&mysql);
while (row = mysql_fetch_row (res))
{
for (r = ; r < mysql_num_fields (res); r++)
{
printf ("%s ", row[r]);
}
printf ("\n");
}
}
else
{
printf ("query failed !\n");
return ; }
mysql_close (&mysql);
return ;
}
.SUFFIXES: .o .c
CC = gcc
SRC = adddata.c
OBJS = $(SRC: .c = .o)
EXEC = Demo .PHONY: start
start: $(OBJS) $(CC) -o $(EXEC) $(OBJS) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient $(OBJS): $(SRC) $(CC) -g -Wall $(OBJS) -c $(SRC) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlcilent .PHONY: clean clean:
rm -f $(OBJS) core.*
#!/bin/bash/ echo "make is starting ....!"
indent -gnu adddata.c make -f adddata.mk
echo "make is endding ...! "
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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "mysql.h"
//#include<fcntl.h> int
main (void)
{ MYSQL mysql; //var mysql
MYSQL_RES *res; //grep_result
MYSQL_ROW row; //mysql_row
int r; //var_tmp r
char *query[];
int flag;
//init_mysql
mysql_init (&mysql);
if (!mysql_real_connect
(&mysql, "localhost", "root", "", "demo", , NULL, ))
{ printf ("can't connect the mysql ! errInfo=[%s]", mysql_error (&mysql)); }
else
{
printf ("connect the mysql successfully!");
query[] = "insert into student(sno,sname)values(1002,'tom')";
query[] = "insert into student(sno,sname)values(1003,'gongxijun')";
query[] = "insert into student(sno,sname)values(1004,'qinshihuang')";
//insert data to mysql
for (r = ; r < ; r++)
{
if (mysql_real_query
(&mysql, query[r], (unsigned int) strlen (query[r])))
{
printf ("insert data[%d] is failed !\n", r);
return ;
}
else
{
printf ("Insert data[%d] is successfully !\n", r);
}
}
} //query part
query[] = "select * from student";
flag =
mysql_real_query (&mysql, query[], (unsigned int) strlen (query[]));
if (!flag)
{
res = mysql_store_result (&mysql);
while (row = mysql_fetch_row (res))
{
for (r = ; r < mysql_num_fields (res); r++)
{
printf ("%s ", row[r]);
}
printf ("\n");
}
}
else
{
printf ("query failed !\n");
return ; }
mysql_close (&mysql);
return ;
}
.SUFFIXES: .o .c
CC = gcc
SRC = adddata.c
OBJS = $(SRC: .c = .o)
EXEC = Demo .PHONY: start
start: $(OBJS) $(CC) -o $(EXEC) $(OBJS) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient $(OBJS): $(SRC) $(CC) -g -Wall $(OBJS) -c $(SRC) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlcilent .PHONY: clean clean:
rm -f $(OBJS) core.*
#!/bin/bash/ echo "make is starting ....!"
indent -gnu adddata.c make -f adddata.mk
echo "make is endding ...! "
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
-------------------------------------华丽丽的分割线-----------------------------------------------------------
|++++++++++++++++++++++显示删除和查询功能++++++++++++++++++++++++++++++++++++|
#include<stdio.h>
#include<string.h>
#include "mysql.h" int
main (void)
{ MYSQL mysql; MYSQL_RES *res; MYSQL_ROW row;
char *query;
int rr, flag;
mysql_init (&mysql); //init mysql
MYSQL *pts = mysql_real_connect (&mysql, "localhost", "root", "", "demo", , NULL, ); //to connect the mysql
if (pts == NULL)
{
printf ("connect is failed ! [%s ]\n", mysql_error (&mysql));
return ;
}
printf ("conected successfully ! \n");
//the part of function is to query the data of mysql query = "select * from student "; flag = mysql_real_query (&mysql, query, (unsigned int) strlen (query));
if (flag)
{
printf ("query the mysql is failed ! [%s ] \n", mysql_error (&mysql));
return ;
} res = mysql_store_result (&mysql); // return a point of res (var MYSQL_RES *) while (row = mysql_fetch_row (res))
{ //to next row for (rr = ; rr < mysql_num_fields (res); rr++)
printf ("[ %s ] ", row[rr]);
puts ("");
}
//the part of del the data of mysql
query = "delete from student where sname = 'gongxijun'";
flag = mysql_real_query (&mysql, query, (unsigned int) strlen (query));
if (flag)
{
printf ("sorry ,delect the data of mysql is failed !\n [%s ] ",
mysql_error (&mysql));
return ;
}
//query again
query = "select * from student ";
flag = mysql_real_query (&mysql, query, (unsigned int) strlen (query));
if (flag)
{
printf ("query failed ! the result= [%s ] \n", mysql_error (&mysql));
return ;
}
res = mysql_store_result (&mysql);
while (row = mysql_fetch_row (res))
{
for (rr = ; rr < mysql_num_fields (res); rr++)
printf ("[%s ]\n", row[rr]);
printf ("\n");
}
mysql_close (&mysql); //close mysql
return ;
}
.SUFFIXES: .o .c
CC = gcc
SRC = del.c
OBJS = $(SRC: .c =.o)
EXEC = Demo .PHONY: start
start: $(OBJS) $(CC) -o $(EXEC) $(OBJS) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient $(OBJS): $(SRC) $(CC) -g -Wall $(OBJS) -c $(SRC) -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient .PHONY: clean
clean:
rm -f $(OBJS) core.* ~
~
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 1.下载MySQL的安装文件 安装MySQL需要下面两个文件: MySQL-server-4.0.16-0.i386.rpm MySQL-cl ...
-
Linux下MySQL的简单操作
Linux下MySQL的简单操作 更改mysql数据库root的密码 首次进入数据库是不用密码的: [root@localhost ~]# /usr/local/mysql/bin/mysql -ur ...
-
linux 下mysql的启动 、调试、排错
Linux 下 MySQL 启动与关闭 说明 一.启动 1.1 MySQL 进程 可以用ps 命令查看进程: [root@rac2 ~]# ps -ef|grep mysql root 21 ...
-
设置Linux下Mysql表名不区分大小写
1.Linux下mysql安装完后是默认:区分表名的大小写,不区分列名的大小写:2.用root帐号登录后,在/etc/my.cnf中的[mysqld]后添加添加lower_case_table_nam ...
-
对于linux下system()函数的深度理解(整理)
原谅: http://blog.sina.com.cn/s/blog_8043547601017qk0.html 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同 ...
-
linux下mysql的表名问题
最近从win转移到了linux,在本机跑好的程序但在linux下一个SQL语句报了错误,发现是表名未找到,错误原因是在linux下mysql的表名是严格区分大小写的.. MYSQL在LINUX下数据库 ...
-
linux下mysql的安装
一.下载 http://dev.mysql.com/downloads/mysql/ 选择对应的版本,这里选择“Linux-Generic” 以64位系统为例,这里需要下载如下两个文件: MySQL- ...
-
转:对于linux下system()函数的深度理解(整理)
这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为 ...
-
【C/C++】Linux下system()函数引发的错误
http://my.oschina.net/renhc/blog/54582 [C/C++]Linux下system()函数引发的错误 恋恋美食 恋恋美食 发布时间: 2012/04/21 11:3 ...
随机推荐
-
Python基础之条件和循环
阅读目录 一.if语句 1.1功能 1.2语法 1.2.1:单分支,单重条件判断 1.2.2:单分支,多重条件判断 1.2.3:if + else 1.2.4:多分支if + elif +else 1 ...
-
ArcGis 创建IWorkspace
); ESRI.ArcGIS.esriSystem.IName name = (ESRI.ArcGIS.esriSystem.IName)workspaceName; ...
-
This application failed to start because it could not find or load the Qt platform plugin “windows”错误解决方法
这是一个困扰我很久的问题,关于Qt下生成的exe文件在没有安装Qt的机器上无法运行的问题.Qt是编写C++图形界面的一个很好工具,比MFC来的直观.可是,Qt的安装却是一个让人头疼的事情.早在上个学期 ...
-
.Net知识点总结(一)
1.文件上传:Jquery.uploadify 它依赖于flash 舍去起上传 功能 改用SWFupload 他是第三方的插件 2.验证码激活的时候,邮箱开始是写死的,但是为了以后更改邮箱 ...
-
玩转图片Base64编码
什么是 base64 编码? 图片的 base64 编码就是可以将一副图片数据编码成一串字符串,使用该字符串代替图像地址. 这样做有什么意义呢?我们知道,我们所看到的网页上的每一个图片,都是需要消耗一 ...
-
oracle的sqlnet.ora , tnsnames.ora , Listener.ora 文件的作用(转)
oracle网络配置三个配置文件 listener.ora.sqlnet.ora.tnsnames.ora ,都是放在$ORACLE_HOME/network/admin目录下.1. sqlnet.o ...
-
JavaWeb——文件上传和下载
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...
-
BZOJ 4008: [HNOI2015]亚瑟王( dp )
dp(i, j)表示考虑了前i张牌, 然后还有j轮的概率. 考虑第i+1张牌: 发动的概率 : p = dp(i, j) * (1 - (1-p[i+1])^j) 没发动的概率 : dp(i, j) ...
-
十个经典的Python面试题
1.Python下多线程的限制以及多进程中传递参数的方式 Python多线程有个全局解释器锁,这个锁的意思是任一时间只能有一个线程运用解释器.并发不是并行. 多进程间同享数据,能够运用multipro ...
-
xtrabackup命令用法实战(转)
xtrabackup命令用法实战 转载出自 https://blog.csdn.net/wfs1994/article/details/80399408 完全备份 1.创建备份 [root@linux ...