在C++中建立到MySQL数据库的简单连接有哪些解决方案?我发现dev.mysql.com的MySQL连接器很难集成。
期待中的感谢!
发布于 2012-07-13 15:36:14
从C/C++应用程序与MySQL通信非常简单
您需要包含mysql.h头文件
连接和执行查询的三个基本API
mysql_connect()
mysql_query()
mysql_close()
链接mysql库(libMysql)
发布于 2012-07-13 16:54:52
您可以使用一个支持库来尝试ODBC路径。
几年前,我使用OTL作为SqlServer的接口,发现它很有效。现在,我已经尝试连接MySql,到目前为止没有任何问题:
#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
复制相似问题