kenzo0619
Posts: 4
Joined: Fri Feb 02, 2018 12:16 am

How to manipulate Mysql through C/C++?

Fri Mar 09, 2018 2:49 pm

I am using php for web-server, however, my was coding mainly in C/C++

I want to reliaze the connection between php and C/C++, I hope they can both store data in Mysql.

Is there any tutorial teach how to use Mysql from C/C++?

thank you so much .

User avatar
DougieLawson
Posts: 32103
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: How to manipulate Mysql through C/C++?

Fri Mar 09, 2018 4:01 pm

Here's a sample program

Code: Select all

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

#define MYSQLHOST "pi.local"
#define MYSQLUSER "picontrol"
#define MYSQLPASSWD "piControlPasswd"
#define MYSQLDB "picontrol"

void finish_with_error(MYSQL *mysqlConn, char * reason) {
        fprintf(stderr, "Err @ %s - %s\n", reason, mysql_error(mysqlConn));
        mysql_close(mysqlConn);
        exit(1);
}

int main(int argc, char **argv) {
        MYSQL *mysqlConn = mysql_init(NULL);
        MYSQL_STMT *stmt;
        MYSQL_BIND param[3];
        MYSQL_BIND result[1];
        MYSQL_ROW row;

        char addr[5];
        unsigned long addr_l = 4;
        char bank[11];
        unsigned long bank_l = 1;
        char channel[4];
        unsigned long channel_l = 1;
        char* status = malloc(4);
        unsigned long status_len = 3;
        const char* a_v;
        a_v = "0x25";
        const char* b_v;
        b_v = "B";
        const char* c_v;
        c_v = "4";
        strncpy(addr,(char*)a_v,4);
        strncpy(bank,(char*)b_v,1);
        strncpy(channel, (char*)c_v,1);

        if (mysqlConn == NULL) {
                fprintf(stderr, "mysql_init() failed\n");
        exit(1);
        }

        if (mysql_real_connect(mysqlConn, MYSQLHOST, MYSQLUSER, MYSQLPASSWD, MYSQLDB, 0, NULL, 0) == NULL) {
                finish_with_error(mysqlConn, "real_connnect");
        }

        char *stmtSQL = " SELECT CASE channel_status WHEN 0 THEN 'off' WHEN 1 THEN 'on' END as status FROM current_status WHERE channel_status <> -1 AND addr = ? AND bank = ? AND channel = ?; ";
        stmt = mysql_stmt_init(mysqlConn);
        if (mysql_stmt_prepare(stmt, stmtSQL, strlen(stmtSQL))) {
                finish_with_error(mysqlConn, "stmt_execute");
        }

        memset(param, 0, sizeof(param));
        param[0].buffer_type = MYSQL_TYPE_STRING;
        param[0].buffer = (char *)addr;
        param[0].buffer_length = sizeof(addr);
        param[0].length = &addr_l;
        param[0].is_null = 0;
        param[1].buffer_type = MYSQL_TYPE_STRING;
        param[1].buffer = (char *)bank;
        param[1].buffer_length = sizeof(bank);
        param[1].length = &bank_l;
        param[1].is_null = 0;
        param[2].buffer_type = MYSQL_TYPE_STRING;
        param[2].buffer = (char *)channel;
        param[2].buffer_length = sizeof(channel);
        param[2].length = &channel_l;
        param[2].is_null = 0;

        if (mysql_stmt_bind_param(stmt, param)) {
                finish_with_error(mysqlConn, "bind_param");
        }

        memset(result, 0, sizeof(result));
        result[0].buffer_type = MYSQL_TYPE_STRING;
        result[0].buffer = status;
        result[0].buffer_length = sizeof(status);
        result[0].length = &status_len;
        if (mysql_stmt_bind_result(stmt, result)) {
                finish_with_error(mysqlConn, "bind_result");
        }

        if (mysql_stmt_execute(stmt) !=0) {
                finish_with_error(mysqlConn, "stmt_execute");
        }
        while (!mysql_stmt_fetch(stmt)) {
                printf("%s\n", status);
        }

        mysql_close(mysqlConn);

        exit(0);
}
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3B*3, 3B+

Please post ALL technical questions on the forum. Do not send private messages.

bazza
Posts: 19
Joined: Thu Apr 17, 2014 8:26 am

Re: How to manipulate Mysql through C/C++?

Fri Mar 09, 2018 8:14 pm

Try mysql++ it makes life quite easy.

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 5 guests