Simple solutions for integrating MySQL with C++?

时光怂恿深爱的人放手 提交于 2021-02-11 12:14:53

问题


What solutions are there for making a simple connection to a MySQL database in C++? I find MySQL Connector from dev.mysql.com hard to integrate.

Anticipated thanks!


回答1:


Its pretty simple to communicate with MySQL from C/C++ application

you need to include mysql.h header file

three basic APIs to connect and execute query

mysql_connect()

mysql_query()

mysql_close()

Link with mysql library (libMysql)




回答2:


You could try the ODBC path, with a support library.

Some year ago I used OTL to interface SqlServer and found it efficient. Now I've tried to interface MySql, without any problem so far:

#include <otlv4.h>
#include <iostream>
using namespace std;

int otl_x_sql_main(int argc, char **argv)
{
    otl_connect db; // connect object
    otl_connect::otl_initialize(); // initialize ODBC environment
    try {
        db.rlogon("DRIVER=mysql;DB=...;UID=...;PWD=..."); // connect to ODBC

        // parametrized SELECT
        otl_stream i(50, "SELECT product_id,model FROM product WHERE product_id >= :f<int> AND product_id < :ff<int>", db);

        int product_id;
        char model[100];

        i << 1000 << 2000; // assigning product_id range

        // SELECT automatically executes when all input variables are assigned
        while (!i.eof()) {
            i >> product_id >> model;
            cout << "product_id=" << product_id << ", model=" << model << endl;
        }
    }
    catch(otl_exception& p) {       // intercept OTL exceptions
        cerr << p.msg << endl;      // print out error message
        cerr << p.stm_text << endl; // print out SQL that caused the error
        cerr << p.sqlstate << endl; // print out SQLSTATE message
        cerr << p.var_info << endl; // print out the variable that caused the error
    }

    return 0;
}


来源:https://stackoverflow.com/questions/11465594/simple-solutions-for-integrating-mysql-with-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!