安装MySQL数据库中获得 MySQL.h 建立C接口的操作流程

数据库 MySQL
今天主要向大家描述的是安装MySQL数据库中获得 MySQL.h 建立C接口的实际操作步骤,下面就是文章的主要内容描述,望你会有所收获。

此文章主要向大家描述的是安装MySQL数据库中获得 MySQL.h 建立C接口的实际操作流程,首先我们是从安装MySQL数据库开始的,其中涉及相关的实际应用代码的描述,下面就是文章的具体内容描述。

先安装MySQL

代码:

  1. sudo apt-get install MySQL-server MySQL-client 

再装开发包

代码:

  1. sudo apt-get install libMySQLclient15-dev 

安装MySQL数据库完以后,C代码里添加头文件

代码:

  1. #include <mysql.h> 

编译方法:

代码:

  1. gcc $(mysql_config --cflags) xxx.c -o xxx $(mysql_config --libs) 

可以用以下代码测试一下

代码:

  1. /* Simple C program that connects to MySQL Database server*/  
  2. #include <mysql.h> 
  3. #include <stdio.h> 
  4. main() {  
  5. MYSQL *conn;  
  6. MYSQL_RES *res;  
  7. MYSQL_ROW row;  
  8. char *server = "localhost";  
  9. char *user = "root";  
  10. char *password = "";   

此处改成你的密码

  1. char *database = "mysql";  
  2. conn = mysql_init(NULL);  
  3. /* Connect to database */  
  4. if (!mysql_real_connect(conn, server,  
  5. user, password, database, 0, NULL, 0)) {  
  6. fprintf(stderr, "%s\n", mysql_error(conn));  
  7. exit(1);  
  8. }  
  9. /* send SQL query */  
  10. if (mysql_query(conn, "show tables")) {  
  11. fprintf(stderr, "%s\n", mysql_error(conn));  
  12. exit(1);  
  13. }  
  14. res = mysql_use_result(conn);  
  15. /* output table name */  
  16. printf("MySQL Tables in mysql database:\n");  
  17. while ((row = mysql_fetch_row(res)) != NULL)  
  18. printf("%s \n", row[0]);  
  19. /* close connection */  
  20. mysql_free_result(res);  
  21. mysql_close(conn);  
  22. }  

 

会输出现有数据库和表内容。以上的相关内容就是对安装MySQL数据库获得 MySQL.h 建立C接口的介绍,望你能有所收获。

【编辑推荐】

  1. MySQL 群集的概念与ndb群集构架图
  2. C#开发MySQL中文乱码的妙招
  3. MySQL 事件调度器示例演示
  4. 实现MySQL数据库备份,很简单!
  5. MySQL匹配模式的实现方案简介

     
责任编辑:佚名 来源: cnblogs
相关推荐

2010-05-20 17:56:43

2010-05-28 18:44:45

2011-03-03 10:00:14

ProFTPD建立MySQL

2010-06-12 09:53:19

2010-05-25 09:47:05

2010-05-26 11:21:00

MySQL数据库操作

2010-05-12 18:02:11

MySQL数据库

2010-06-12 17:48:45

MySQL数据库表

2010-05-28 13:48:07

MySQL数据库密码

2019-10-21 08:08:34

MySQL数据库主键

2010-06-04 18:45:00

MySQL数据库

2010-05-11 13:50:56

MySQL数据库

2010-06-01 09:32:09

MySQL数据库

2011-07-05 18:04:45

QT Mysql

2010-05-24 14:02:06

MySQL数据库

2010-06-10 08:48:14

2010-06-04 10:59:54

MySQL数据库返回影

2010-06-01 13:58:24

远程连接MySQL

2009-02-03 13:06:17

日常维护规范MySQL

2010-05-24 18:56:15

点赞
收藏

51CTO技术栈公众号