24,854
社区成员
发帖
与我相关
我的任务
分享
test_is_empty = 0;
cout << "ip:" << config.ip << endl;
cout << "user:" << config.user << endl;
cout << "passwd:" << config.passwd << endl;
cout << "database:" << config.database << endl;
cout << "port:" << config.port << endl;
if (mysql_real_connect(&connection, config.ip.c_str(), config.user.c_str(), config.passwd.c_str(), config.database.c_str(), config.port, NULL, 0)) {
cout << "test_is_empty:" << test_is_empty << endl;
i_conn = 1;
class MysqlClient {
..
private:
MYSQL connection;
int test_is_empty;
MYSQL_RES *result;
db_info config;
std::string error_msg;
};
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <iostream>
#include "mysql_client.h"
using namespace std;
int MysqlClient::Connect() {
test_is_empty = 0;
if (!mysql_init(&connection))
{
cout << "mysql_init error. ret:" << string(mysql_error(&connection)) << endl;
}
if (!mysql_real_connect(&connection,
"192.168.111.120",
"root",
"123",
"db_session_",
3306, NULL, 0))
{
cout << "mysql_real_connect error. ret:" << string(mysql_error(&connection)) << endl;
}
cout << "test_is_empty:" << test_is_empty << endl;
return 0;
}
mysql_client.h文件:
#ifndef _MYSQL_CLIENT_H
#define _MYSQL_CLIENT_H
#include <string>
#include <pthread.h>
#include "mysql.h"
#include "mysqld_error.h"
class MysqlClient {
public:
int Connect();
private:
MYSQL connection;
int test_is_empty;
};
#endif
main.cpp
#include "mysql_client.h"
int main(void)
{
MysqlClient mysql;
mysql.Connect();
return 0;
}
最后输出尽然是:
test_is_empty:37115232