MySQL连接函数崩溃
MySQL Crashing in the connect function
我有x64位,下载了32位(那是因为lib文件很少出错,所以我必须使用32位)这里都做了:http://dev.mysql.com/doc/refman/5.6/en/connector-cpp-apps-windows-visual-studio.html#connector-cpp-application-build-dynamic
但是程序在'connect'函数处崩溃:
http://imageshack.us/photo/my-images/827/25478716.jpg/该怎么办?这是我试图编译的源代码。
#include <stdlib.h>
#include <iostream>
#include <Windows.h>
/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
//__declspec(dllexport)
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' » AS _message'..." << endl;
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "pass");//
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next()) {
cout << "t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("_message") << endl;
cout << "t... MySQL says it again: ";
/* Access column fata by numeric offset, 1 is the first column */
cout << res->getString(1) << endl;
}
delete res;
delete stmt;
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
cout << endl;
return EXIT_SUCCESS;
}
你可以尝试用不同的方式构建连接:
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
// just going to input the general details and not the port numbers
struct connection_details
{
char *server;
char *user;
char *password;
char *database;
};
MYSQL* mysql_connection_setup(struct connection_details mysql_details)
{
// first of all create a mysql instance and initialize the variables within
MYSQL *connection = mysql_init(NULL);
// connect to the database with the details attached.
if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) {
printf("Conection error : %sn", mysql_error(connection));
exit(1);
}
return connection;
}
MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
{
// send the query to the database
if (mysql_query(connection, sql_query))
{
printf("MySQL query error : %sn", mysql_error(connection));
exit(1);
}
return mysql_use_result(connection);
}
int main()
{
MYSQL *conn; // the connection
MYSQL_RES *res; // the results
MYSQL_ROW row; // the results row (line by line)
struct connection_details mysqlD;
mysqlD.server = "localhost"; // where the mysql database is
mysqlD.user = "mysqlusername"; // the root user of mysql
mysqlD.password = "mysqlpassword"; // the password of the root user in mysql
mysqlD.database = "mysql"; // the databse to pick
// connect to the mysql database
conn = mysql_connection_setup(mysqlD);
// assign the results return to the MYSQL_RES pointer
res = mysql_perform_query(conn, "show tables");
printf("MySQL Tables in mysql database:n");
while ((row = mysql_fetch_row(res)) !=NULL)
printf("%sn", row[0]);
/* clean up the database result set */
mysql_free_result(res);
/* clean up the database link */
mysql_close(conn);
return 0;
}
查看此处完整示例
相关文章:
- 内联映射初始化的动态atexit析构函数崩溃
- 执行函数时导致崩溃的变量
- C 回调函数在对象 pascal 上崩溃
- 除非使用某些寄存器,否则函数挂钩会崩溃
- Linux c++.在预加载的共享库中定义的基类的崩溃调用函数
- Rcpp 函数在打包时使 RStudio 崩溃
- 使用 MS 绕道挂钩在调用钩子函数时崩溃C++
- 矢量迭代器在尝试调用函数时使我的程序崩溃
- 使用唯一指针调用函数会使我的程序崩溃
- 钩/绕道 d3d9 (现在/结束场景) - 似乎调用我的函数然后崩溃
- 程序在为链表创建推送函数时崩溃
- 递归导致程序崩溃,但 while 循环中的相同概念不是来自C++析构函数
- 从 C# 模块调用C++函数引发随机崩溃
- C++设置器函数崩溃
- EGL 在第一次 opengl 函数调用时崩溃
- 我需要做一个空函数循环回来,而不会让它崩溃或跳过
- 当我使用 ALSA lib 的函数"snd_pcm_readi"时它崩溃了
- 在C++中删除指针数组时析构函数崩溃
- 使用向量的函数崩溃,调试器说访问冲突读取位置
- 我的单例中的数组在离开函数后没有保留信息,然后在尝试再次访问信息时崩溃