在 Xcode 中使用 C++ 我尝试使用 MySQL Connector/C++ 访问 MySQL 数据库。问题是程序(用 Xcode 编译)总是崩溃
EXC_BAD_ACCESS (code=13, address=0x0)
打电话时
driver->connect(url, user, pass)
在 Xcode 中,我创建了一个完整的新项目(OS X > 命令行工具),在 main.cpp 中插入了代码(见下文),添加了 Boost 和 MySQL 连接器标头包含路径以及 libmysqlcppconn.6.1.1.1.dylib 作为链接库并点击运行按钮。
接下来是,当我使用手动编译程序时
c++ -o test -I /usr/local/mysqlConnector/include/ -lmysqlcppconn main.cpp
该程序运行良好,并且还在表上运行 INSERT 语句。
程序代码取自 MySQL Connector/C++ 示例,即 pthreads.cpp 示例,但被截断为基本部分:
/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
std::string url;
std::string user;
std::string pass;
std::string database;
/**
* Usage example for Driver, Connection, (simple) Statement, ResultSet
*/
int main(int argc, const char **argv)
{
sql::Driver *driver;
std::auto_ptr< sql::Connection > con;
url = "tcp://127.0.0.1:3306";
user = "appserver";
pass = "testpw";
database = "appserver";
try {
driver = sql::mysql::get_driver_instance();
/* Using the Driver to create a connection */
con.reset(driver->connect(url, user, pass));
con->setSchema(database);
sql::Statement* stmt = con->createStatement();
stmt->execute("INSERT INTO testtable (testnumber) values (5)");
} catch (sql::SQLException &e) {
return EXIT_FAILURE;
} catch (std::runtime_error &e) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}