如何用C语言编写程序访问并查询MySQL数据库?

2026-05-20 20:221阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计467个文字,预计阅读时间需要2分钟。

如何用C语言编写程序访问并查询MySQL数据库?

原文示例:

本文实例讲述了C语言实现访问及查询MySQL数据库的方法。分享给广大程序员参考,具体如下:

如何用C语言编写程序访问并查询MySQL数据库?

1. 添加头文件路径(MySQL安装路径中的include路径)

2.添加库文件路径(MySQL安装路径中的lib路径)

3.包含MySQL头文件

4.初始化MySQL连接

5.连接数据库

6.创建游标

7.执行SQL语句

8.获取查询结果

9.关闭连接

代码示例:

c

#include

int main() { MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row;

conn=mysql_init(NULL); if (!mysql_real_connect(conn, localhost, root, password, testdb, 0, NULL, 0)) { fprintf(stderr, %s\n, mysql_error(conn)); return 1; }

res=mysql_query(conn, SELECT * FROM users); if (!res) { fprintf(stderr, %s\n, mysql_error(conn)); return 1; }

while ((row=mysql_fetch_row(res)) !=NULL) { printf(%s\n, row[0]); }

mysql_free_result(res); mysql_close(conn);

return 0;}

本文实例讲述了C语言实现访问及查询MySQL数据库的方法。分享给大家供大家参考,具体如下:

1、添加头文件路径(MySQL安装路径中的include路径)
2、添加库文件(直接从MySQL安装路径中copy libmysql.lib即可)
3、编程操作数据库

代码

// AccessToMySQL.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <Windows.h> #include <mysql.h> #pragma comment(lib,"libmysql.lib") MYSQL mysql; MYSQL_RES* result; MYSQL_ROW row; int main(void) { //init the mysql parameter mysql_init(&mysql); //connect the database if(!mysql_real_connect(&mysql,"127.0.0.1","root","111","mytest",3306,NULL,0)) { printf(mysql_error(&mysql)); printf("\nCannot access to the database!!!\n"); system("pause"); exit(-1); } //construct the query SQL statements char* sql="select * from student where name='"; char dest[100]={""}; strcat(dest,sql); printf("Please enter the student name:"); char name[10]={""}; gets(name); strcat(dest,name); strcat(dest,"'"); //excute the SQL statements if(mysql_query(&mysql,dest)) { printf("Cannot access the database with excuting \"%s\".",dest); system("pause"); exit(-1); } //deal with the result result=mysql_store_result(&mysql); if(mysql_num_rows(result)) { while((row=mysql_fetch_row(result))) { printf("%s\t%s\t%s\n",row[0],row[1],row[2]); } } //release the resource mysql_free_result(result); mysql_close(&mysql); system("pause"); return 0; }

运行效果:

希望本文所述对大家C语言程序设计有所帮助。

标签:方法

本文共计467个文字,预计阅读时间需要2分钟。

如何用C语言编写程序访问并查询MySQL数据库?

原文示例:

本文实例讲述了C语言实现访问及查询MySQL数据库的方法。分享给广大程序员参考,具体如下:

如何用C语言编写程序访问并查询MySQL数据库?

1. 添加头文件路径(MySQL安装路径中的include路径)

2.添加库文件路径(MySQL安装路径中的lib路径)

3.包含MySQL头文件

4.初始化MySQL连接

5.连接数据库

6.创建游标

7.执行SQL语句

8.获取查询结果

9.关闭连接

代码示例:

c

#include

int main() { MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row;

conn=mysql_init(NULL); if (!mysql_real_connect(conn, localhost, root, password, testdb, 0, NULL, 0)) { fprintf(stderr, %s\n, mysql_error(conn)); return 1; }

res=mysql_query(conn, SELECT * FROM users); if (!res) { fprintf(stderr, %s\n, mysql_error(conn)); return 1; }

while ((row=mysql_fetch_row(res)) !=NULL) { printf(%s\n, row[0]); }

mysql_free_result(res); mysql_close(conn);

return 0;}

本文实例讲述了C语言实现访问及查询MySQL数据库的方法。分享给大家供大家参考,具体如下:

1、添加头文件路径(MySQL安装路径中的include路径)
2、添加库文件(直接从MySQL安装路径中copy libmysql.lib即可)
3、编程操作数据库

代码

// AccessToMySQL.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <Windows.h> #include <mysql.h> #pragma comment(lib,"libmysql.lib") MYSQL mysql; MYSQL_RES* result; MYSQL_ROW row; int main(void) { //init the mysql parameter mysql_init(&mysql); //connect the database if(!mysql_real_connect(&mysql,"127.0.0.1","root","111","mytest",3306,NULL,0)) { printf(mysql_error(&mysql)); printf("\nCannot access to the database!!!\n"); system("pause"); exit(-1); } //construct the query SQL statements char* sql="select * from student where name='"; char dest[100]={""}; strcat(dest,sql); printf("Please enter the student name:"); char name[10]={""}; gets(name); strcat(dest,name); strcat(dest,"'"); //excute the SQL statements if(mysql_query(&mysql,dest)) { printf("Cannot access the database with excuting \"%s\".",dest); system("pause"); exit(-1); } //deal with the result result=mysql_store_result(&mysql); if(mysql_num_rows(result)) { while((row=mysql_fetch_row(result))) { printf("%s\t%s\t%s\n",row[0],row[1],row[2]); } } //release the resource mysql_free_result(result); mysql_close(&mysql); system("pause"); return 0; }

运行效果:

希望本文所述对大家C语言程序设计有所帮助。

标签:方法