linux 下尝试c与mysql编程 author:loconfuse url=http://blog.csdn.net/loconfuse 写一段访问mysql的代码非常的简单,可以从随mysql源码发布的代码包中截取。 #fileName=test.c #include "stdio.h" #include "mysql.h" //某些情况下需要将该头文件copy到目录下 main() { MYSQL *ssock;//有些变量或者函数不熟悉的话,可以在mysql.h中查,明白参数的意义 char execsql[500]; ssock=(MYSQL *)malloc(sizeof(MYSQL)); mysql_init(ssock)//在某些版本中,该初始化工作是不需要的,可观看mysql.H,以及read me if(ssock==NULL) { printf("init error!\n"); exit(1); } //connect to selected db ssock=mysql_real_connect(ssock,"localhost","username","password","dbname",0,NULL,0); if (!ssock) { printf("cannot connect to the mysql_server \n"); exit(-1); } mysql_select_db(ssock,"test"); //exec my execsql string sprintf(execsql,"create table girls (name char(10),age int)"); mysql_real_query(ssock,execsql,strlen(execsql)); mysql_close(ssock); } 在Emacs中编辑完成保存,开始编译,可以采用makefile方式,但是作为学习,先从顺序编译开始。 gcc -c -I/usr/local/inlude/mysql test.c gcc -o test test.o -L/usr/local/lib/mysql -lmysqlclient [-lm] [-lz] 后面附带的参数可选,毕竟在安装的环境中还是有很大的库函数存放位置差异的。 
|