#include "QWiseSqlDatabase.h"
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include <QDebug>
QWiseSqlDatabase::QWiseSqlDatabase(QObject *parent)
:QObject(parent)
{
//qDebug()<<QSqlDatabase::drivers();
}
bool QWiseSqlDatabase::open(const QString &name,
const QString &user,
const QString &password,
const QString &host,
int port)
{
bool status = true;
m_db = QSqlDatabase::addDatabase("QMYSQL");
m_db.setDatabaseName( name );
m_db.setUserName( user );
m_db.setPassword( password );
m_db.setHostName( host );
m_db.setPort(port);
m_name = name;
m_user = user;
m_password = password;
m_host = host;
m_port = port;
if (!m_db.open())
{
qDebug()<<"Failed to open database: "
<<m_db.lastError().driverText()
<<m_db.lastError().databaseText();
status = false;
}
return status;
}
QWiseSqlDatabase::~QWiseSqlDatabase()
{
close();
}
void QWiseSqlDatabase::close()
{
if (m_db.isOpen())
{
m_db.close();
}
}
bool QWiseSqlDatabase::insert(const QString &table, QStringList &names, QStringList &values, int &numRowsAffected)
{
if (names.size() != values.size())
return false;
QSqlQuery query(QSqlDatabase::database());
QString sql = QString("insert into ") + table + QString("(");
for (int i = 0; i < names.size(); i++)
{
sql = sql + names.value(i);
if (i != names.size() - 1)
{
sql+=QString(",");
}
else
{
sql = sql + QString(")");
}
}
sql = sql + QString("values (");
for (int i = 0; i < values.size(); i++)
{
sql = sql + QString("'") + values.value(i) + QString("'");
if (i != values.size()-1)
{
sql = sql + QString(",");
}
}
sql = sql + QString(")");
bool status = query.exec(sql);
numRowsAffected = query.numRowsAffected();
return status;
}
bool QWiseSqlDatabase::insert(const QString &table, QStringList &names, QStringList &values)
{
int numRowsAffected;
return insert(table, names, values, numRowsAffected);
}
bool QWiseSqlDatabase::update(const QString &table, QStringList &names, QStringList &values, QString &expression)
{
if (names.size() != values.size())
{
return false;
}
//UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
QSqlQuery query(QSqlDatabase::database());
QString sql = QString("update ")+table+QString(" set ");
for (int i = 0; i < names.size(); i++)
{
sql = sql + names.value(i);
sql = sql + QString(" = '");
sql = sql + values.value(i);
sql = sql + QString("'");
if (i != names.size()-1)
{
sql = sql + QString(" ,");
}
}
sql = sql + QString(" ") + expression;
return query.exec(sql);
}
bool QWiseSqlDatabase::del(const QString &table, QString &expression)
{
//DELETE FROM 表名称 WHERE 列名称 = 值
QSqlQuery query(QSqlDatabase::database());
QString sql = QString("delete from ") + table + QString(" ") + expression;
return query.exec(sql);
}
//条件查询数据库
bool QWiseSqlDatabase::select(const QString &table, QStringList &names, QVector<QStringList> &values, QString &expression, int &numRowsAffected)
{
if (!(names.size() > 0))
return false;
QSqlQuery query(QSqlDatabase::database());
QString sql;
for (int i = 0; i < names.size(); i++)
{
if (i < (names.size() - 1))
{
sql += names.value(i) + QString(" ,");
}
else
{
sql += names.value(i);
}
}
sql = QString("select %1 from %2").arg(sql).arg(table);
if (!expression.isEmpty())
sql += QString(" where ") + expression;
qDebug()<<sql;
bool status = query.exec(sql);
while (query.next())
{
QStringList value;
for (int i = 0; i < names.size(); i++)
{
value << query.value(i).toString();
}
values.push_back(value);
}
numRowsAffected = query.size();
return status;
}
bool QWiseSqlDatabase::select(const QString &table, QStringList &names, QVector<QStringList> &values, int &numRowsAffected)
{
return select(table, names, values, QString(""), numRowsAffected);
}