Инструменты пользователя

Инструменты сайта


works:programmer:cpp:mysql

Установка и работа с MySQL на C++

makefile
make: main.cpp
	g++ -Wall -o xvec main.cpp -lmariadb
 
clean:
	rm -rf xvec *.o
main.cpp
#include <iostream>
#include <vector>
#include <string>
 
#include <mariadb/mysql.h>
 
using namespace std;
 
vector<string> f;
 
void fail(MYSQL* c) {
    fprintf(stderr, "%s\n", mysql_error(c));
    mysql_close(c);
    exit(1);
}
 
int main() {
 
    MYSQL* conn = mysql_init(NULL);
    if (conn == NULL) {
        cerr << "mysql_init failed!" << endl;
        return 1;
    }
 
    if ( mysql_real_connect(conn, "localhost", "root", "datasuit86!", "homeauto", 0, NULL, 0) == NULL) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        mysql_close(conn);
        return 1;
    }
 
    if ( mysql_query(conn, "SELECT CURRENT_TIMESTAMP as 'time', version() as 'ver' UNION SELECT now(), 6543456546546456456456")) {
        fail(conn);
    }
 
    MYSQL_RES* result = mysql_store_result(conn);
    if (result == NULL) {
        fail(conn);
    }
 
    // int num_fields = mysql_num_fields(result);
    MYSQL_ROW row;
 
    while ((row = mysql_fetch_row(result))) {
 
        cout << row[0] << ", " << row[1] << endl;
 
    }
 
    mysql_free_result(result);
 
    mysql_close(conn);
 
    cout << "OK!" << endl;
    return 0;
}
works/programmer/cpp/mysql.txt · Последнее изменение: 2019/12/10 18:40 — 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki