c直接能像其他语言比如:php那样直接连接mysql数据库吗?求代码例子!
网上一堆一堆的/* Simple C program that connects to MySQL Database server*/
#include <mysql.h>#include <stdio.h>main() {MYSQL *conn;MYSQL_RES *res;MYSQL_ROW row;char *server = "localhost";char *user = "root";char *password = "PASSWORD"; /* set me first */char *database = "mysql";conn = mysql_init(NULL);/* Connect to database */if (!mysql_real_connect(conn, server,user, password, database, 0, NULL, 0)) {fprintf(stderr, "%sn", mysql_error(conn));exit(1);}/* send SQL query */if (mysql_query(conn, "show tables")) {fprintf(stderr, "%sn", mysql_error(conn));exit(1);}res = mysql_use_result(conn);/* output table name */printf("MySQL Tables in mysql database:n");while ((row = mysql_fetch_row(res)) != NULL)printf("%s n", row[0]);/* close connection */mysql_free_result(res);mysql_close(conn);}
去官网下一份www.mysql.com,或者在安装目录的mysqlinclude目录下找到mysql.h就行了,代码如下:
#if defined(_WIN32) || defined(_WIN64) //为了支持windows平台上的编译#include <windows.h>#endif#include <stdio.h>#include <stdlib.h>#include "mysql.h"
//定义数据库操作的宏,也可以不定义留着后面直接写进代码#define SELECT_QUERY "show tables;"
int main(int argc, char **argv) //char **argv 相当于 char *argv[]{MYSQL mysql,*handle; //定义数据库连接的句柄,它被用于几乎所有的MySQL函数MYSQL_RES *result; //查询结果集,结构类型MYSQL_FIELD *field ; //包含字段信息的结构MYSQL_ROW row ; //存放一行查询结果的字符串数组char querysql[160]; //存放查询sql语句字符串//初始化mysql_init(&mysql);//连接数据库if (!(handle = mysql_real_connect(&mysql,"localhost","user","pwd","dbname",0,NULL,0))) {fprintf(stderr,"Couldn't connect to engine!n%snn",mysql_error(&mysql));}sprintf(querysql,SELECT_QUERY,atoi(argv[1]));//查询数据库if(mysql_query(handle,querysql)) {fprintf(stderr,"Query failed (%s)n",mysql_error(handle));}//存储结果集if (!(result=mysql_store_result(handle))) {fprintf(stderr,"Couldn't get result from %sn", mysql_error(handle));}printf("number of fields returned: %dn",mysql_num_fields(result));//读取结果集的内容while (row = mysql_fetch_row(result)) {printf("table: %sn",(((row[0]==NULL)&&(!strlen(row[0]))) ? "NULL" : row[0]) ) ;}//释放结果集mysql_free_result(result);//关闭数据库连接mysql_close(handle);
system("PAUSE");//为了兼容大部分的编译器加入此行return 0;}
网上很多,你可以百度搜索一下,例子比如http://wenku.baidu.com/view/657006946bec0975f465e231.htmlhttp://www.vckbase.com/bbs/prime/viewprime.asp?id=69http://zhidao.baidu.com/question/292045383.html
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(3)
网上一堆一堆的
/* Simple C program that connects to MySQL Database server*/
#include <mysql.h>
#include <stdio.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "PASSWORD"; /* set me first */
char *database = "mysql";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%sn", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%sn", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
去官网下一份www.mysql.com,或者在安装目录的mysqlinclude目录下找到mysql.h就行了,代码如下:
#if defined(_WIN32) || defined(_WIN64) //为了支持windows平台上的编译
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"
//定义数据库操作的宏,也可以不定义留着后面直接写进代码
#define SELECT_QUERY "show tables;"
int main(int argc, char **argv) //char **argv 相当于 char *argv[]
{
MYSQL mysql,*handle; //定义数据库连接的句柄,它被用于几乎所有的MySQL函数
MYSQL_RES *result; //查询结果集,结构类型
MYSQL_FIELD *field ; //包含字段信息的结构
MYSQL_ROW row ; //存放一行查询结果的字符串数组
char querysql[160]; //存放查询sql语句字符串
//初始化
mysql_init(&mysql);
//连接数据库
if (!(handle = mysql_real_connect(&mysql,"localhost","user","pwd","dbname",0,NULL,0))) {
fprintf(stderr,"Couldn't connect to engine!n%snn",mysql_error(&mysql));
}
sprintf(querysql,SELECT_QUERY,atoi(argv[1]));
//查询数据库
if(mysql_query(handle,querysql)) {
fprintf(stderr,"Query failed (%s)n",mysql_error(handle));
}
//存储结果集
if (!(result=mysql_store_result(handle))) {
fprintf(stderr,"Couldn't get result from %sn", mysql_error(handle));
}
printf("number of fields returned: %dn",mysql_num_fields(result));
//读取结果集的内容
while (row = mysql_fetch_row(result)) {
printf("table: %sn",(((row[0]==NULL)&&(!strlen(row[0]))) ? "NULL" : row[0]) ) ;
}
//释放结果集
mysql_free_result(result);
//关闭数据库连接
mysql_close(handle);
system("PAUSE");
//为了兼容大部分的编译器加入此行
return 0;
}
网上很多,你可以百度搜索一下,例子比如http://wenku.baidu.com/view/657006946bec0975f465e231.html
http://www.vckbase.com/bbs/prime/viewprime.asp?id=69
http://zhidao.baidu.com/question/292045383.html