linux下c语言学习笔记——操作mysql
linux_c吧
全部回复
仅看楼主
level 4
spirit_medium 楼主
1,[比较详细]在 C 里嵌入 SQL:http://www.pgsqldb.org/pgsqldoc-7.4/ecpg.html2,[在MySQL数据库中使用C执行SQL语句]:http://www.dvbbs.net/tech/data/2006031818989.asp3,MySQL客户工具和API:http://www.yesky.com/imagesnew/software/mysql/manual_Clients.html4,基于mysql的高性能数据库应用开发:http://cache.baidu.com/c?word=mysql%3B%5F%3Breal%3B%5F%3Bconnect%2C%B2%CE%CA%FD&url=http%3A//www%2Edaima%2Ecom%2Ecn/Info/76/Info27780/&b=0&a=2&user=baidu注:下面的所有例子在mandriva linux下测试通过
2007年05月28日 15点05分 1
level 4
spirit_medium 楼主
1,使用c语言操作mysql之前,先在mysql里头创建一个数据库,一个表,在表里头添加数据如下:创建数据库,库名为cusemysql:mysql>create database cusemysql;创建表,表名为:mysql>use cusemysql;mysql>create table children(childno int not null unique,fname varchar(20),age int);添加一点数据哦:mysql>insert into children values(5,"花儿",10);对拉,为了方便起见,把表的大致样子给大家看看childno fname age1 小星 92 大量 15
2007年05月28日 15点05分 2
level 4
spirit_medium 楼主
2 ,下面进行具体的操作插入:insert 好的,我们现编辑一段c代码,取名为insert.cCode:/* insert.c */#include
#include
#include "/usr/local/mysql/include/mysql/mysql.h"/*注意哦,上面必须是mysql.h的绝对地址,一般在mysql下的include目录下,仔细
看看你的
在哪里?*/int main(int argc, char *argv[]){ MYSQL my_connection; int res; mysql_init(&my_connection);/*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/ if (mysql_real_connect(&my_connection, "localhost", "root", "","cusemysql",0,NULL,CLIENT_FOUND_ROWS)) { printf("Connection success\n"); res = mysql_query(&my_connection, "insert into children values(10,'Ann',5)"); if (!res) { printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection)); /*里头的函数返回受表中影响的行数*/ } else { fprintf(stderr, "Insert error %d: s\n",mysql_errno,(&my_connection),mysql_error(&my_connection)); } mysql_close(&my_connection); } else { fprintf(stderr, "Connection failed\n"); if (mysql_errno(&my_connection)) { fprintf(stderr, "Connection error %d: %s\n", mysql_errno(&my_connection), mysql_error(&my_connection)); } }return EXIT_SUCCESS;}
2007年05月28日 15点05分 3
level 4
spirit_medium 楼主
代码写完了,要编译哦#gcc -o insert insert.c -L /usr/local/mysql/lib/mysql/*.a -
lz
如果上边的编译不成功,可以试一下下边的#gcc -o insert insert.c `mysql_config --cflags --libs`ok,现在我们执行看看#./insertConnection SuccessInserted 1 rowsyear,果然可以,呵呵不信到mysql下看看表children中是否多了刚才插入的那一行数据注:也许你会问上面gcc的命令参数是什么意思阿,其实,我也不太清楚,呵呵大概是要把mysql下的某个特定库包含进来,可是我不知道具体是个什么库,所以用*.a全部包含进来拉其实只要包含mysqlclient.a就可以,你试试看
2007年05月28日 15点05分 4
level 4
spirit_medium 楼主
更新:update我们只要把上面的代码中的res = mysql_query(&my_connection, "insert into children values(10,'Ann',5)");换成res = mysql_query(&my_connection, "update children set age=20 where childno<5 ");即可上面语句实现的功能是,把编号小于5的所有孩子的年龄全部改成20岁检索:select看代码之前,最好是先看蓝色字体的部分〔介绍了代码中用到的一些函数的作用〕
2007年05月28日 15点05分 5
level 4
spirit_medium 楼主
Code:/* select.c */#include
#include
#include "/usr/local/mysql/include/mysql/mysql.h"int main(int argc, char *argv[]){ MYSQL my_connection; MYSQL_RES *res_ptr; MYSQL_ROW sqlrow; int res; mysql_init(&my_connection); /*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/ if (mysql_real_connect(&my_connection, "localhost", "root", "","cusemysql",0,NULL,CLIENT_FOUND_ROWS)){ printf("Connection success\n"); res = mysql_query(&my_connection, "select childno,fname,age from children where age<20"); if (res) { printf("SELECT error:%s\n",mysql_error(&my_connection)); } else { res_ptr=mysql_store_result(&my_connection); if(res_ptr) { printf("Retrieved %lu Rows\n",(unsigned long)mysql_num_rows(res_ptr)); while((sqlrow=mysql_fetch_row(res_ptr))) { printf("Fetched data...\n"); } if (mysql_errno(&my_connection)) { fprintf(stderr,"Retrive error:s\n",mysql_error(&my_connection)); } } mysql_free_result(res_ptr); } mysql_close(&my_connection); }else{ fprintf(stderr, "Connection failed\n"); if (mysql_errno(&my_connection)) { fprintf(stderr, "Connection error %d: %s\n", mysql_errno(&my_connection), mysql_error(&my_connection)); } } return EXIT_SUCCESS;}
2007年05月28日 15点05分 6
level 4
spirit_medium 楼主
上面语句实现的功能是:检索出年龄小于20岁的小孩的信息,不过没有对信息进行任何处理哦下次我们对数据进行一定的处理这里介绍上面用到的几个函数: 可以从SELECT语句(或其他返回数据的语句)中检索完所有数据,在单一调用中,使用mysql_store_result: MYSQL_RES *mysql_store_result(MYSQL *connection); 必须在mysql_query检索数据后才能调用这个函数,以在结果集中存储该数据。这个函数从服务器中检索所有数据并立即将它存储在客户机中。它返回一个指向以前我们从未遇到过的结构(结果集结构)的指针。如果语句失败,则返回NULL。 使用等价的PostgreSQL时,应该知道返回NULL意味着已经发生了错误,并且这与未检索到数据的情况不同。即使,返回值不是NULL,也不意味着当前有数据要处理。 如果未返回NULL,则可以调用mysql_num_rows并且检索实际返回的行数,它当然可能是0。 my_ulonglong mysql_num_rows(MYSQL_RES *result); 它从mysql_store_result取得返回的结果结构,并且在该结果集中返回行数,行数可能为0。如果mysql_store_result成功,则mysql_num_rows也总是成功的。
2007年05月28日 15点05分 7
level 4
spirit_medium 楼主
这种mysql_store_result和mysql_num_rows的组合是检索数据的一种简便并且直接的方法。一旦 mysql_store_result成功返回,则所有查询数据都已经存储在客户机上并且我们知道可以从结果结构中检索它,而不用担心会发生数据库或网络错误,因为对于程序所有数据都是本地的。还可以立即发现返回的行数,它可以使编码更简便。如前所述,它将所有结果立即地发送回客户机。对于大结果集,它可能耗费大量的服务器、网络和客户机资源。由于这些原因,使用更大的数据集时,最好仅检索需要的数据。不久,我们将讨论如何使用 mysql_use_result函数来完成该操作。 一旦检索了数据,则可以使用mysql_fetch_row来检索它,并且使用mysql_data_seek、mysql_row_seek、mysql_row_tell操作结果集。在开始检索数据阶段之前,让我们先讨论一下这些函数。 MYSQL_ROW mysql_fetch_row(MYSQL_RES *result); 这个函数采用从存储结果中获取的结果结构,并且从中检索单一行,在行结构中返回分配给您的数据。当没有更多数据或者发生错误时,返回NULL。稍后,我们将回来处理这一行中的数据。 void mysql_data_seek(MYSQL_RES *result, my_ulonglong offset); 这个函数允许您进入结果集,设置将由下一个获取操作返回的行。offset是行号,它必须在从0到结果集中的行数减 1 的范围内。传递0将导致在下一次调用mysql_fetch_row时返回第一行。 MYSQL_ROW_OFFEST mysql_row_tell(MYSQL_RES *result); 这个函数返回一个偏移值,它表示结果集中的当前位置。它不是行号,不能将它用于mysql_data_seek。但是,可将它用于: MYSQL_ROW_OFFSET mysql_row_seek(MYSQL_RES *result, MYSQL_ROW_OFFSET offset); 它移动结果集中的当前位置,并返回以前的位置。 有时,这一对函数对于在结果集中的已知点之间跳转很有用。请注意,不要将row tell和row seek使用的偏移值与data_seek使用的行号混淆。这些是不可交换的,结果将是您所希望看到的。 void mysql_free_result(MYSQL_RES *result); 完成结果集时, 必须总是调用这个函数,以允许MySQL库整理分配给它的对象。 检索并处理[比较全面哦,呵呵]:select
2007年05月28日 15点05分 8
3Q 转载到我空间去
2012年07月05日 08点07分
level 4
spirit_medium 楼主
下面是详细的代码:Code:/* select1.c */#include
#include
#include
#include "/usr/local/mysql/include/mysql/mysql.h"int main(int argc, char *argv[]){ MYSQL my_connection; MYSQL_RES *res_ptr; /*指向检索的结果存放地址的指针*/ MYSQL_ROW sqlrow; /*返回的记录信息*/ MYSQL_FIELD *fd; /*字段结构指针*/ char aszflds[25][25]; /*用来存放各字段名*/ int res; /*执行查询操作后的返回标志*/ int i,j,k; mysql_init(&my_connection); /*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/ if (mysql_real_connect(&my_connection, "localhost", "root", "","cusemysql",0,NULL,CLIENT_FOUND_ROWS)){ printf("Connection success\n"); res = mysql_query(&my_connection, "select childno,fname,age from children where age<20"); if (res) { printf("SELECT error:%s\n",mysql_error(&my_connection)); } else { res_ptr=mysql_store_result(&my_connection); if(res_ptr) { printf("Retrieved %lu Rows\n",(unsigned long)mysql_num_rows(res_ptr)); /*取得各字段名*/ for(i=0;fd=mysql_fetch_field(res_ptr);i++) strcpy(aszflds
,fd->name); /*输出各条记录*/ printf("下面是检索出的各条记录信息:\n"); j=mysql_num_fields(res_ptr); for(i=0;i
); printf("\n"); while((sqlrow=mysql_fetch_row(res_ptr))) { for(i=0;i
); printf("\n"); } if (mysql_errno(&my_connection)) { fprintf(stderr,"Retrive error:s\n",mysql_error(&my_connection)); } } mysql_free_result(res_ptr); } mysql_close(&my_connection); }else{ fprintf(stderr, "Connection failed\n"); if (mysql_errno(&my_connection)) { fprintf(stderr, "Connection error %d: %s\n", mysql_errno(&my_connection), mysql_error(&my_connection)); } } return EXIT_SUCCESS;}
2007年05月28日 15点05分 9
level 0
[return@localhost ~]$ ls /usr/include/mysql/chardefs.h my_config_i386.h my_semaphore.h my_xml.h sslopt-case.herrmsg.h my_dbug.h mysql_com.h raid.h sslopt-longopts.hhistory.h my_dir.h mysqld_ername.h readline.h sslopt-vars.hkeycache.h my_getopt.h mysqld_error.h rlmbutil.h tilde.hkeymaps.h my_global.h mysql_embed.h rlprivate.h typelib.hm_ctype.h my_list.h mysql.h rlshell.h xmalloc.hm_string.h my_net.h mysql_time.h rltypedefs.hmy_alloc.h my_no_pthread.h mysql_version.h sql_common.hmy_config.h my_pthread.h my_sys.h sql_state.h[return@localhost ~]$ ls /usr/local/mysql/include/mysql/ls: /usr/local/mysql/include/mysql/: 没有那个文件或目录[return@localhost ~]$-----------谢谢大虾...问一下,linux怎么查找文件的?我找mysql.h这个文件找了半年.....
2007年11月12日 01点11分 10
level 1
爱你 木阿木阿木阿
2010年09月15日 03点09分 16
level 1
我是打酱油的路过
2011年03月16日 03点03分 17
level 1
好贴
2011年04月18日 04点04分 19
level 1
神贴!
2012年01月12日 02点01分 20
level 4
[Yeah]加油……
2012年06月06日 07点06分 21
1