在 C 中使用 MariaDB [英] Using MariaDB in C

查看:29
本文介绍了在 C 中使用 MariaDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C 脚本连接到 MariaDB 数据库,但找不到必要的文档.我安装了 libmariadbclient-dev,但找不到任何随附的文档,例如手册页.here 有基本描述和有限的文档,但是文档仅包括功能描述.事实是,尽管搜索了各种谷歌结果,我什至不知道要导入什么才能让它工作,更不用说如何使用它了.有没有关于如何在 C 中使用 MariaDB 数据库的指南或文档?

I'm trying to connect to a MariaDB database in a C script and I can't find the necessary documentation. I installed libmariadbclient-dev, but I couldn't find any accompanying documentation such as a man page. There's a basic description and limited documentation here, but the documentation only includes descriptions of functions. The fact is, despite having scoured all sorts of Google results, I don't even know what to import to get this to work, much less how to use it. Is there any guide or documentation on how to use a MariaDB database in C?

推荐答案

C 的 MariaDB 客户端库具有与 MySQL 完全相同的 APIMySQL 5.5 的连接器/C

The MariaDB Client Library for C has exactly the same API as the MySQL Connector/C for MySQL 5.5

这里是:http://dev.mysql.com/doc/refman/5.5/en/c-api-function-overview.html

另一个:http://zetcode.com/db/mysqlc/

你可以编译一个最小的测试,比如

You can compile a minimal test like

#include <my_global.h>
#include <mysql.h>

int main(int argc, char **argv)
{  
  MYSQL *con = mysql_init(NULL);

  if (con == NULL) 
  {
      fprintf(stderr, "%s
", mysql_error(con));
      exit(1);
  }

  if (mysql_real_connect(con, "localhost", "root", "root_pswd", 
          NULL, 0, NULL, 0) == NULL) 
  {
      fprintf(stderr, "%s
", mysql_error(con));
      mysql_close(con);
      exit(1);
  }  

  if (mysql_query(con, "CREATE DATABASE testdb")) 
  {
      fprintf(stderr, "%s
", mysql_error(con));
      mysql_close(con);
      exit(1);
  }

  mysql_close(con);
  exit(0);
}

使用

gcc -o mysql-test mysql-test.c $(mysql_config --libs)

这篇关于在 C 中使用 MariaDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆