Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MariaDB in C

Tags:

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?

like image 553
Mike Avatar asked Jun 23 '13 21:06

Mike


People also ask

What can I do with MariaDB?

The MariaDB database is used for various purposes such as data warehousing, e-commerce, enterprise-level features, and logging applications. MariaDB will efficiently enable you to meet all your workload; it works in any cloud database and works at any scale – small or large. What is a database?

What is MariaDB programming?

MariaDB is a database system, a database server. To interface with the MariaDB server, you can use a client program, or you can write a program or script with one of the popular programming languages (e.g., PHP) using an API (Application Programming Interface) to interface with the MariaDB server.

Does MySQL ODBC work with MariaDB?

Yes, MySQL's Connector/J is compatible with MariaDB.


1 Answers

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

Here it is: http://dev.mysql.com/doc/refman/5.5/en/c-api-function-overview.html

Another one: 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\n", mysql_error(con));
      exit(1);
  }

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

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

  mysql_close(con);
  exit(0);
}

using

gcc -o mysql-test mysql-test.c $(mysql_config --libs)
like image 151
David Ranieri Avatar answered Oct 16 '22 22:10

David Ranieri