重构了连接数据库、设置设备名的代码,封装为类

This commit is contained in:
2024-07-29 00:37:14 +08:00
parent 1a300f4ca7
commit 6cca0535bc
6 changed files with 162 additions and 32 deletions

83
databaseAPI.h Normal file
View File

@@ -0,0 +1,83 @@
#ifndef DATABASEAPI_H
#define DATABASEAPI_H
#include <QSqlDatabase>
#include <QString>
class Database
{
private:
QSqlDatabase db;
bool connected = false;
QString databaseName = QString("cardManageSystem");
QString userName = QString("cardManageSystem");
public:
Database(QSqlDatabase database)
{
db = database;
db.setDatabaseName(databaseName);
db.setUserName(userName);
}
Database(QSqlDatabase database, QString hostName, int port, QString password)
{
db = database;
db.setDatabaseName(databaseName);
db.setUserName(userName);
db.setHostName(hostName);
db.setPort(port);
db.setPassword(password);
}
QSqlDatabase getDatabase()
{
return db;
}
void setHostName(QString hostName)
{
db.setHostName(hostName);
}
QString getHostName()
{
return db.hostName();
}
void setPort(int port)
{
db.setPort(port);
}
int getPort()
{
return db.port();
}
void setPassword(QString password)
{
db.setPassword(password);
}
bool is_connected()
{
return connected;
}
bool open()
{
connected = db.open();
return connected;
}
~Database()
{
db.close();
}
};
#endif // DATABASEAPI_H