emsApplication/applications/emsConfigurer/mysqlutils.cpp

148 lines
3.5 KiB
C++
Raw Normal View History

2024-09-21 13:57:16 +08:00
#include "mysqlutils.h"
2024-10-11 14:20:12 +08:00
#include <QDebug>
2024-09-21 13:57:16 +08:00
#include <hv/hlog.h>
2024-10-11 14:20:12 +08:00
#ifdef _USING_MYSQL_CONNECTOR_
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#endif
2024-09-21 13:57:16 +08:00
MysqlUtils MysqlUtils::m_instance;
MysqlUtils::MysqlUtils()
2024-10-11 14:20:12 +08:00
#ifdef _USING_MYSQL_CONNECTOR_
:m_DbConnection(nullptr)
#endif
#ifdef _USING_MYSQL_51_LIB_
:m_DbConnection(nullptr)
#endif
2024-09-21 13:57:16 +08:00
{}
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)
{
2024-10-11 14:20:12 +08:00
bool ok = false;
#ifdef _USING_QT_MYSQL_CONNECTOR_
2024-09-21 13:57:16 +08:00
// 打开数据库
2024-10-11 14:20:12 +08:00
if (m_DbConnection.isOpen() && m_DbConnection.isValid())
2024-09-21 13:57:16 +08:00
{
2024-10-11 14:20:12 +08:00
hlogd("database already connected.");
return true;
}
2024-09-21 13:57:16 +08:00
2024-10-11 14:20:12 +08:00
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
2024-09-21 13:57:16 +08:00
2024-10-11 14:20:12 +08:00
#ifdef _USING_MYSQL_CONNECTOR_
// 创建连接
sql::Driver *driver = nullptr;
driver = get_driver_instance();
2024-09-21 13:57:16 +08:00
2024-10-11 14:20:12 +08:00
sql::ConnectOptionsMap connection_properties;
2024-09-21 13:57:16 +08:00
2024-10-11 14:20:12 +08:00
driver->connect("tcp://192.168.23.253:3306", "root", "L2ysc1s1kr");
m_DbConnection->setSchema(database.c_str());
2024-09-21 13:57:16 +08:00
2024-10-11 14:20:12 +08:00
// 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;
2024-09-21 13:57:16 +08:00
2024-10-11 14:20:12 +08:00
// m_DbConnection = driver->connect(connection_properties);
2024-09-21 13:57:16 +08:00
2024-10-11 14:20:12 +08:00
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.");
2024-09-21 13:57:16 +08:00
}
2024-10-11 14:20:12 +08:00
else
2024-09-21 13:57:16 +08:00
{
2024-10-11 14:20:12 +08:00
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
2024-09-21 13:57:16 +08:00
}
2024-10-11 14:20:12 +08:00
return ok;
2024-09-21 13:57:16 +08:00
}
void MysqlUtils::CloseDatabase()
{
2024-10-11 14:20:12 +08:00
#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
2024-09-21 13:57:16 +08:00
}