How to set up and use MariaDB Connector C

I just wanted to provide quick tutorial on using MariaDB’s Connector C.

I downloaded the latest Connector C (3.0.2), running MariaDB 10.1.28, and was able to get it to work fine with a simple C program using the following commands:

1. Downloaded the Connector C .msi file (32-bit, since my VS is 32-bit), extracted, and installed, which placed it at:

C:\Program Files (x86)\MariaDB\MariaDB Connector C\lib

2. You need to add the directory that contains libmaria.dll to your $PATH Environment LIB PATH variable. In my case, it was:

Control Panel -> System -> Advanced System Settings -> Environment Variables -> Choose "LIB" from under "System variables" -> then add the Connector C lib path, like:
C:\Program Files (x86)\MariaDB\MariaDB Connector C\lib

3. Then just run the following command, where my c/c++ program name is “mysql1.c”:

cl /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" /I "C:\Program Files (x86)\mariadb-10.1.25\include\mysql" /I "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include" /MD "C:\Program Files (x86)\MariaDB\MariaDB Connector C\lib\libmariadb.lib" mysql1.c

Note the first path is to include all normal C headers like stdio.h, the second for mysql.h, and the third is for windows.h, and the last for the Connector C .lib.

Here is the actual session output:

C:\chris\mysql1> cl /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" /I "C:\Program Files (x86)\mariadb-10.1.25\include\mysql" /I "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include" /MD "C:\Program Files (x86)\MariaDB\MariaDB Connector C\lib\libmariadb.lib" mysql1.c

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

mysql1.c
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.

/out:mysql1.exe
C:\chris\mysql1\libmariadb.lib
mysql1.obj

4. If all is successful, as the above, you can invoke your newly created c/c++ program, mysql1.exe, in this case (mine just obtains the version number from the instance):

C:\chris\mysql1>mysql1

MySQL Version = 100128

For reference, here is my mysql1.c code:

#include 
#include 
#include 
 
MYSQL *conn;
int version = 1;

int main ( int argc, char *argv[] )
{
    conn = mysql_init ( NULL );
    mysql_real_connect ( conn, "localhost", "root",
            "xxxxx", "test", 3316, NULL, 0 );
	version = mysql_get_server_version( conn );
	printf("\nMySQL Version = %d\n",version);
    mysql_close ( conn );
    return 0;
}

Previous related posts, if interested:

  1. Creating a basic C/C++ Program to Interact with MySQL and MariaDB
  2. Common Errors and Resolutions for Building your own MySQL or MariaDB C/C++ Program on Windows
  3. Setting Up Connector/C and SkySQL C Connector for MySQL and MariaDB

I hope this helps.

Creating a basic C/C++ Program to Interact with MySQL and MariaDB

In this post, I’ll cover how to create a simple C/C++ program that interacts with MySQL using the C API (and Connector/C). I discussed in a previous post how to install the C Connector (both SkySQL and MySQL’s), so please refer to that if you are looking for more specifics on that.

So once you have that set up, then there are just a few more things you need to have in order – at least this was the case in my environment.

1. Create a program (this one simply connects to the server and retrieves the server version):

#include <stdio.h>
#include <windows.h>
#include <mysql.h>
 
MYSQL *conn;
int version = 1;

int main ( int argc, char *argv[] )
{
    conn = mysql_init ( NULL );
    mysql_real_connect ( conn, "localhost", "root",
            "password", "test", 3308, NULL, 0 );
	version = mysql_get_server_version( conn );
	printf("\nMySQL Version = %d\n",version);
    mysql_close ( conn );
    return 0;
}

I saved it as c:\chris\mysql1\mysql1.c.

2. I copied libmysql.dll and libmysql.lib to c:\chris\mysql1
     (There’s probably a better way than this, but this works for now.)

3. cd c:\chris\mysql1\

4. Compile your program (I’m compiling from the command line, using cl.exe):

cl /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" 
/I "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\include" 
/I "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include" 
/MD "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\lib\libmysql.lib" 
mysql1.c

You need the various includes specified by the /I, and the /MD indicates we’re compiling it dynamically (as opposed to statically – /MT).

5. Run Program, by just typing the name (from the same location) – “mysql1” in this case:

mysql1

Here is the full session output:

c:\chris\mysql1>
cl /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" 
/I "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\include" 
/I "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include" 
/MD "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\lib\libmysql.lib" 
mysql1.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 
16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

mysql1.c
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:mysql1.exe
"C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\lib\libmysql.lib"
mysql1.obj

c:\chris\mysql1>mysql1

MySQL Version = 50532

c:\chris\mysql1>

Fwiw, I did run into a number of errors, but I’ve posted that in a follow-up, so as to keep this post more focused.

Hope this helps.

 

Setting Up Connector/C and SkySQL C Connector for MySQL and MariaDB

I’m writing a post on how to create your first C/C++ program for MySQL (using Windows, and from the command line). A prerequisite for that is to have a C Connector, such as MySQL Connector/C or SkySQL C Connector, so the program can communicate with mysqld.

I didn’t want that post to be too scattered, so I decided to split it into two, more focused posts. That said, this first post will focus on the Connector, and the next post will actually cover the program itself.

Installing and using Connector/C with MySQL is quite simple, so I wanted to show how easy it is. I also wanted to show examples with both SkySQL C Connector with MariaDB (which also works with MySQL) and Connector/C with MySQL, since both are widely used. I also wanted to show some common errors one might encounter and their resolutions, so hopefully this will help anyone who might have issues along the way.

SkySQL C Connector & MariaDB:

1. Download SkySQL C Connector and extract to:

C:\Program Files\MySQL\mariadb_client-1.0.0

2. Test the Connector per the Docs:

cd C:\Program Files\MariaDB 5.5\bin
mysql_client_test -uroot -p -P3314

or:

"C:\Program Files\MariaDB 5.5\bin\mysql_client_test" -uroot -p -P3314

Connector/C & MySQL:

1. Download Connector/C extract to:

C:\Program Files\MySQL\MySQL Connector C 6.1

2. Test the Connector per the Docs:

cd C:\Program Files\MySQL\MySQL Server 5.5\bin
mysql_client_test -uroot -p -P3308

or:

"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql_client_test" -uroot -p -P3308

Outputs:

SkySQL C Connector & MariaDB:

#####################################
client_connect  
#####################################

 Establishing a connection to '' ...OK
Connected to MySQL server version: 5.5.31-MariaDB (50531)

 Creating a test database 'client_test_db' ...OK

#####################################
1 of (1/1): test_view_sp_list_fields  
#####################################
...
tons of test output
...

Connector/C & MySQL:

#####################################
client_connect  
#####################################

 Establishing a connection to '' ...OK
Connected to MySQL server version: 5.5.32-log (50532)

 Creating a test database 'client_test_db' ...OK

#####################################
1 of (1/1): test_view_sp_list_fields  
#####################################
...
tons of test output
...

Errors & Fixes:

This was very straight-forward overall, and I only ran into 2 quite basic issues, but want to share them here for completeness and in case anyone else runs into the same:

1. When I first invoked mysql_client_test, I received the following error:

mysql_client_test: unknown variable 'default-character-set=utf8'

This just means I had ‘default-character-set=utf8’ set under the [client] section in my config file, and apparently this option cannot be read by mysql_client_test. The simple fix is to add the “loose-” prefix to this option in the config file:

[client]
loose-default-character-set=utf8

Now, you can run the file without encountering this error. Instead, you’ll just see a “warning” now about “unknown variable ‘loose-default-character-set=utf8′”. 🙂

Note there is *no* need to restart mysqld after making this change, since the change only applies ‘clients’ and not the server (i.e., mysqld).

2. The only other error I could stumble into is:

Establishing a connection to '' ... [MySQL] connection failed

And that was just because I invoked mysql_client_test without –user and –password options.

Hope this helps. 🙂