You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
3.5 KiB
C++
166 lines
3.5 KiB
C++
#include "databasesql.h"
|
|
#include "gensql.h"
|
|
#define _MD_PRIMARY_KEY "uid"
|
|
#define _QMYSQL_ "QMYSQL"
|
|
#define _SQLITE_ "QSQLITE"
|
|
|
|
DataBaseSql::DataBaseSql(const QString& dbName, const QString &dbType)
|
|
{
|
|
m_DBType = dbType;
|
|
if (dbType == "QSQLITE")
|
|
{
|
|
db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE", dbName));
|
|
db->setDatabaseName(dbName);
|
|
//qry = QSqlQuery::QSqlQuery(db);
|
|
}
|
|
else if (dbType == "QMYSQL")
|
|
{
|
|
//if (!QSqlDatabase::contains(m_DBType))
|
|
db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL", dbName));
|
|
//db.setDatabaseName(dbName);
|
|
}
|
|
}
|
|
|
|
DataBaseSql::~DataBaseSql()
|
|
{
|
|
if (db->isOpen())
|
|
{
|
|
db->close();
|
|
}
|
|
delete db;
|
|
db = NULL;
|
|
}
|
|
|
|
bool DataBaseSql::openDB()
|
|
{
|
|
return db->open();
|
|
}
|
|
|
|
void DataBaseSql::closeDB()
|
|
{
|
|
db->close();
|
|
}
|
|
|
|
bool DataBaseSql::InitDatabase()
|
|
{
|
|
if (!db->open() && db->open())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool DataBaseSql::CreatTable(QString m_tableName, QString m_primaty, QVariantMap m_map)
|
|
{
|
|
QString strSql;
|
|
if (m_DBType == _SQLITE_)
|
|
strSql = gensql::genCreateTabel(m_tableName, m_primaty, "INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL", m_map);
|
|
else if (m_DBType == _QMYSQL_)
|
|
strSql = QString("CREATE TABLE `testtable2` (`id` int(11) NOT NULL,`name` varchar(60) DEFAULT NULL,`age` int(11) DEFAULT NULL,PRIMARY KEY(`id`)) ENGINE = InnoDB DEFAULT CHARSET = utf8; ");
|
|
QSqlQuery sql = db->exec(strSql);
|
|
|
|
QSqlError err = sql.lastError();
|
|
int nType = err.type();
|
|
if (nType != QSqlError::NoError)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
bool DataBaseSql::InsertOneData(QString m_tableName, QVariantMap m_map)
|
|
{
|
|
|
|
if (m_tableName.isEmpty() || m_map.empty()) {
|
|
return false;
|
|
}
|
|
QString strSql;
|
|
strSql = gensql::genInsertData(m_tableName, m_map);
|
|
qry = db->exec(strSql);
|
|
QSqlError bflags = qry.lastError();
|
|
int nType = bflags.type();
|
|
QString str = db->databaseName();
|
|
if (db->lastError().isValid())
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool DataBaseSql::ModefyOneData(QString m_tableName, QVariantMap m_map)
|
|
{
|
|
if (m_tableName.isEmpty() || m_map.empty()) {
|
|
return false;
|
|
}
|
|
int max_id = 0;
|
|
QString select_max_sql = "select max(md_unique) from e_report_forms";
|
|
QSqlQuery sql = db->exec(select_max_sql);
|
|
if (db->lastError().isValid())
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
while (sql.next())
|
|
{
|
|
max_id = sql.value(0).toInt();
|
|
}
|
|
}
|
|
QString m = gensql::genClass(_MD_PRIMARY_KEY, "0");
|
|
QString strSql = gensql::genUpdate(m_tableName, m_map, m);
|
|
|
|
db->exec(strSql);
|
|
if (db->lastError().isValid())
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool DataBaseSql::DeleteOneData(QString m_tableName, QString m_map)
|
|
{
|
|
if (m_tableName.isEmpty() || m_map.isEmpty()) {
|
|
return false;
|
|
}
|
|
int max_id = 0;
|
|
QString select_max_sql = "select max(" + QString(_MD_PRIMARY_KEY) + ") from " + m_tableName;
|
|
QSqlQuery sql = db->exec(select_max_sql);
|
|
if (db->lastError().isValid())
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
while (sql.next())
|
|
{
|
|
max_id = sql.value(0).toInt();
|
|
}
|
|
}
|
|
QString m;// = genClass(_MD_PRIMARY_KEY, "0");
|
|
QString strSql = gensql::genDeleteData(m_tableName, m);
|
|
|
|
db->exec(strSql);
|
|
if (db->lastError().isValid())
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void DataBaseSql::SetDatabaseName(QString dbName)
|
|
{
|
|
db->setDatabaseName(dbName);
|
|
}
|
|
|
|
void DataBaseSql::SetDBPort(QString nAddr /*= QString("localhost")*/, int nPort /*= 3306*/)
|
|
{
|
|
db->setHostName(nAddr);
|
|
db->setPort(nPort);
|
|
}
|
|
|
|
void DataBaseSql::SetDBUser(QString dbUser /*= QString("root")*/, QString dbPwd /*= QString("hzleaper")*/)
|
|
{
|
|
db->setUserName(dbUser);
|
|
db->setPassword(dbPwd);
|
|
}
|
|
|