相关文章推荐
Hi, I have a maria db that works perfectly. I can use the db with a connection from a Windows 10 pc through a visual client or through visual studio C# program. From windows, I can use the same procedures like for mysql db and I can add, delete, table, view or simply add record. Now I would like to implement a program in c to be compiled with gcc on the Raspberry in order to add records on one db table. On the net I find only examples for mysql, but it does not seem to be the right way because I can't compile the source file:
test02.c:1:19: fatal error: mysql.h: No such file or directory
#include <mysql.h>
. How can I write a program in c and compile it with gcc that connects to the db and write a record?
Thank you very much
RaspBerry 3 b+
RASPBIAN STRETCH WITH DESKTOP
Image with desktop based on Debian Stretch
Version:March 2018
Release date:2018-03-13
Kernel version:4.9
S.Fredella
Turin, IT EC
I believe you need to install the libmariadb-dev-compat package, but I haven't actually tested this .
Update: I installed libmariadb-dev-compat and I could connect from a C program running on my RPI3+ to a MariaDB server running on my Windows PC.
Thank for You, I have installed libmariadb-dev-compat package. I have found the correct path of Mysql.h
find /usr/ -name 'mysql.h'
/usr/include/mariadb/mysql.h
Then I try to compile with this command:
root@raspberrypi:/home/pi# gcc -I/usr/include/mariadb/ test02.c
/tmp/ccULTCjW.o: In function `finish_with_error':
test02.c:(.text+0x20): undefined reference to `mysql_error'
test02.c:(.text+0x3c): undefined reference to `mysql_close'
/tmp/ccULTCjW.o: In function `main':
test02.c:(.text+0x68): undefined reference to `mysql_init'
test02.c:(.text+0x150): undefined reference to `mysql_real_connect'
test02.c:(.text+0x178): undefined reference to `mysql_stmt_init'
test02.c:(.text+0x198): undefined reference to `mysql_stmt_prepare'
test02.c:(.text+0x24c): undefined reference to `mysql_stmt_bind_param'
test02.c:(.text+0x2a8): undefined reference to `mysql_stmt_bind_result'
test02.c:(.text+0x2c8): undefined reference to `mysql_stmt_execute'
test02.c:(.text+0x2f4): undefined reference to `mysql_stmt_fetch'
test02.c:(.text+0x308): undefined reference to `mysql_close'
collect2: error: ld returned 1 exit status
root@raspberrypi:/home/pi#
The code inside test02.c

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);
Something is still missing to hook up the library.
Languages using left-hand whitespace for syntax are ridiculous
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
See the documentation on building MySQL C API client programs on Linux: https://dev.mysql.com/doc/refman/5.7/en ... ients.html
Use the mysql_config program to get the correct compiler and linker flags:

Code: Select all

gcc `mysql_config --cflags --libs` test02.c
 
推荐文章