DelphiFAQ Home Search:

Stub to connect from a C program to mysql


commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.


I need a code stub to connect from a C program to mysql. I have never used the mysql C API before.


The following should get you started. The source code is 'loader.c'.

Compile it with

gcc loader.c -o loader -I /usr/local/mysql/include -L /usr/local/mysql/lib -l mysqlclient

If you get error messages, check where your mysql is installed. Make sure that mysqlclient is on the lib search path.

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

int mysql_exec_sql(MYSQL *mysql,const char *create_definition) {
  return mysql_real_query(mysql,create_definition,strlen(create_definition));

int main() {
  MYSQL *mysql=NULL;
  MYSQL_RES *result;
  MYSQL_ROW row;
  char sQuery[1000];
  unsigned int num_fields;
  int i;

  if((mysql=mysql_init(mysql))==NULL) {
    printf("\nFailed to initate MySQL connection");

  if (!mysql_real_connect(mysql,"","root","","qos",0,NULL,0)) {
    printf( "Failed to connect to MySQL: Error: %s\n", mysql_error(mysql));

  printf("Logged on to database sucessfully");

  strcpy(sQuery, "select count(*) from traffic");

  if(mysql_exec_sql(mysql,sQuery)==0) {
    result = mysql_store_result(mysql);
    // are there any rows??
    if (result) {
      num_fields = mysql_num_fields(result);
      while ((row = mysql_fetch_row(result))) {
        for(i = 0; i < num_fields; i++) {
          printf("[%s] ", row[i]);



Content-type: text/html


2012-06-05, 21:29:07
anonymous from China  
<strong> http://www.coachout..strong> <a href=' http://www.coachout..g>Coach Outlet Online</strong></a><br />
<strong> http://www.coachfac..strong> <a href=' http://www.coachfac..g>Coach Factory</strong></a><br />
<strong> http://www.myloveco..strong> <a href=' http://www.myloveco..g>Coach Outlet Online</strong></a><br />
<strong> http://www.buyincoa..strong> <a href=' http://www.buyincoa..g>Coach Outlet</strong></a><br />
<strong> http://www.cheapcoa..strong> <a href=' http://www.cheapcoa..g>Cheap Coach Handbags</strong></a><br />
<strong> http://www.coachfac..strong> <a href=' http://www.coachfac..g>Coach Factory Outlet</strong></a><br />
<strong> http://www.buycoach..strong> <a href=' http://www.buycoach..g>Coach Outlet Online</strong></a><br />
<strong></strong> <a href=' http://www.coachout..g>Coach Factory Outlet</strong></a><br />
<strong></strong> <a href=' http://www.coachout..g>Coach Outlet Online</strong></a><br />
<strong></strong> <a href=' http://www.coachfac..g>Coach Factory</strong></a><br />
<strong></strong> <a href=' http://www.gotocoac..g>Coach Outlet Online</strong></a><br />
2012-11-12, 02:52:21
anonymous from United Kingdom  
2013-01-27, 19:46:23
anonymous from China  



NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: