mysql_demo_02.cpp

minimal example

//==================================================================================
// title   : mysql_demo_02.cpp 
// author  : Neil Rieck
// edit    : 2024-03-11
// purpose : ensure we can compile/link against the mysql client libraries using c++
// platform: Linux (CentOS-7)
// build   : g++ $(mysql_config --cflags --include --libs) \
// 		mysql_demo_02.cpp -o mysql_demo_02
// execute : ./mysql_demo_02
//==================================================================================
#include <iostream>
#include <string>
#include <mysql/mysql.h>			// various mysql definitions
#include <stdio.h>				// stderr is found here

const char* program  = "mysql_demo_02.cpp";
const char* hostname = "kawc4n.on.bell.ca";	// or "localhost"
const char* username = "neil";
const char* password = "not-my-password";
const char* database = "mysql";

using namespace std;

MYSQL mysql;

int main(void){
    std::cout << "-i-program: " << program << std::endl;
    //=================================================
    std::cout << "-i-test 001" << std::endl;
    std::cout << "-i-MySQL client version: ";
    std::cout << mysql_get_client_info()  << std::endl;
    //=================================================
    std::cout << "-i-test 002" << std::endl;
    mysql_init(&mysql);
    //=================================================
    std::cout << "-i-test 003" << std::endl;
    if (!mysql_real_connect(&mysql, hostname,
			    username, password,
			    database, 3306, NULL,0)){
	fprintf(stderr,
		"-e-Failed to connect to database: Error: %s\n",
		mysql_error(&mysql));
    }else{
	std::cout << "-i-Successfully connected." << std::endl;
    }
    //=================================================
    std::cout << "-i-test 004 (normal exit)" << std::endl;
    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.