#include "mysqlutils.h" #include #include #ifdef _USING_MYSQL_CONNECTOR_ #include #include #include #include #include #endif MysqlUtils MysqlUtils::m_instance; MysqlUtils::MysqlUtils() #ifdef _USING_MYSQL_CONNECTOR_ :m_DbConnection(nullptr) #endif #ifdef _USING_MYSQL_51_LIB_ :m_DbConnection(nullptr) #endif {} MysqlUtils::~MysqlUtils() { CloseDatabase(); } MysqlUtils* MysqlUtils::getInstance() { return &m_instance; } bool MysqlUtils::OpenDatabase(const std::string& server, int dbport, const std::string& dbuser, const std::string& dbpasswd, const std::string& database) { bool ok = false; #ifdef _USING_QT_MYSQL_CONNECTOR_ // 打开数据库 if (m_DbConnection.isOpen() && m_DbConnection.isValid()) { hlogd("database already connected."); return true; } m_DbConnection = QSqlDatabase::addDatabase("QMYSQL"); m_DbConnection.setHostName(server.c_str()); m_DbConnection.setDatabaseName(database.c_str()); m_DbConnection.setUserName(dbuser.c_str()); m_DbConnection.setPassword(dbpasswd.c_str()); m_DbConnection.setPort(dbport); ok = m_DbConnection.open(); #endif #ifdef _USING_MYSQL_CONNECTOR_ // 创建连接 sql::Driver *driver = nullptr; driver = get_driver_instance(); sql::ConnectOptionsMap connection_properties; driver->connect("tcp://192.168.23.253:3306", "root", "L2ysc1s1kr"); m_DbConnection->setSchema(database.c_str()); // connection_properties["hostName"] = server; // connection_properties["userName"] = dbuser; // connection_properties["password"] = dbpasswd; // connection_properties["schema"] = database; // connection_properties["port"] = dbport; // //connection_properties["OPT_RECONNECT"] = true; // m_DbConnection = driver->connect(connection_properties); if (m_DbConnection) ok = true; #endif #ifdef _USING_MYSQL_51_LIB_ m_DbConnection = mysql_init(NULL); // 连接到MySQL服务器 if (!mysql_real_connect(m_DbConnection, server.c_str(), dbuser.c_str(), dbpasswd.c_str(), database.c_str(), dbport, NULL, 0)) { hloge("MySQL connection error: %s",mysql_error(m_DbConnection)); } else { ok = true; mysql_set_character_set(m_DbConnection, "utf8"); } #endif if (!ok) { hloge("Failed to connect to database."); } else { hlogd("new database connection created successfully!"); #if 0 MYSQL_RES* res; MYSQL_ROW row; // 执行 SQL 查询 if (mysql_query(m_DbConnection, "SELECT * FROM tbl_device")) { qDebug() << "Error: " << mysql_error(m_DbConnection) ; mysql_close(m_DbConnection); return 1; } res = mysql_use_result(m_DbConnection); // 获取结果集 while ((row = mysql_fetch_row(res)) != NULL) { for (int i = 0; i < mysql_num_fields(res); ++i) { qDebug() << row[i] << "\t"; } qDebug() << "\r\n"; } mysql_free_result(res); // 释放结果集资源 #endif } return ok; } void MysqlUtils::CloseDatabase() { #ifdef _USING_QT_MYSQL_CONNECTOR_ if (m_DbConnection.isOpen()) m_DbConnection.close(); #endif #ifdef _USING_MYSQL_CONNECTOR_ if (m_DbConnection) { m_DbConnection->close(); } #endif #ifdef _USING_MYSQL_51_LIB_ mysql_close(m_DbConnection); // 关闭连接 #endif }