【问题标题】:c language with mysql( how to include mySQL database into program )c语言与mysql(如何将mySQL数据库包含到程序中)
【发布时间】:2016-06-26 08:15:40
【问题描述】:

我想将 MySQL 数据库集成到我的 C 程序中。 有没有可能的方法呢?应该使用哪些库? (如果可能的话)

【问题讨论】:

  • 您是如何将所需的库链接到您的程序的?它们是什么格式的?
  • #include
  • 这是某种巨魔帖子吗?
  • 你为什么这么说哈?
  • 这不是将预定义函数包含到C语言中的方式吗?

标签: mysql sql c database


【解决方案1】:

C API 代码与 MySQL 一起分发。它包含在 mysqlclient 库中,允许 C 程序访问数据库。

MySQL 源代码分发中的许多客户端都是用 C 编写的。如果您正在寻找演示如何使用 C API 的示例,请查看这些客户端。您可以在 MySQL 源代码分发的 clients 目录中找到这些。

这是一个连接 MySQL 服务器并列出数据库中所有表的小程序:

#include <mysql.h>
#include <stdio.h>

int main(void) {
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;
  /* Change me */
   char *server = "localhost";
   char *user = "root";
   char *password = "PASSWORD";
   char *database = "mysql";

   conn = mysql_init(NULL);

   /* Connect to database */
   if (!mysql_real_connect(conn, server,
         user, password, database, 0, NULL, 0)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
   }

   /* send SQL query */
   if (mysql_query(conn, "show tables")) {
      fprintf(stderr, "%s\n", 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);

  return 0;
}

【讨论】:

  • 我不明白这是如何回答这个问题的,因为他的编译器甚至找不到库。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-15
  • 2017-05-14
  • 2017-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多