mysql_demo_01.c

minimal example

// ==============================================================================
// title  : mysql_demo_01.c
// author : Neil Rieck
// edit   : 2024-03-11
// purpose: ensure we can compile/link against the mysql client libraries using c
// ref    : https://dev.mysql.com/doc/c-api/8.0/en/mysql-real-connect.html
// platfom: Linux (CentOS-7) 
// build  : gcc $(mysql_config --cflags --include --libs) \
// 		mysql_demo_01.c -o mysql_demo_01
// execute: ./mysql_demo_01
// ==============================================================================

#include <stdlib.h>
#include <stdio.h>
#include <mysql.h>				// various mysql definitions

const char* program  = "mysql_demo_01.c";
const char* hostname = "kawc4n.on.bell.ca";
const char* username = "neil";
const char* password = "not-my-password";
const char* database = "mysql";

int main(){
    printf("-i-program %s\n", program);
    printf("-i-test 001\n");
    printf("-i-MySQL client version: %s\n", mysql_get_client_info());
    //=================================
    printf("-i-test 002\n");
    MYSQL* conn;
    if (!(conn = mysql_init(NULL)))
    {
        puts("Initialization has failed!");
        return 1;
    }
    //=================================
    printf("-i-test 003\n");
    if (conn = mysql_real_connect(
		conn,
		hostname,
		username,
		password,
		database, 3306, NULL, 0))
    {
        puts("-i-Success.");
    }else{
        puts("-e-Failed to connect!");
    }
    //=================================
    printf("-i-test 0004 (normal exit)\n");
    return 0;
}

left hand Back to Linux Demo Index
left hand Back to Real-world Linux Problems + Solutions
home Back to Home
Neil Rieck
Waterloo, Ontario, Canada.