0 Star 0 Fork 3

fujingyuan/AutoVehTypeMon

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mydatebase.cpp 3.62 KB
一键复制 编辑 原始数据 按行查看 历史
惊险一血 提交于 2014-10-28 10:39 . 首次提交
#include "mydatebase.h"
#include <QSqlQuery>
#include <QVariant>
#include <QDebug>
#include <QSqlError>
#include <QFile>
#include "logmsg.h"
#include <QCryptographicHash>
MyDatebase::MyDatebase(QObject *parent) :
QObject(parent)
{
m_DBName = "./QtNet.dll";
}
bool MyDatebase::InitDB()
{
QFile file(m_DBName);
if(!file.exists())
{
//qDebug()<<tr("数据库:%1不存在,进行自动创建").arg(m_DBName);
if(!file.open(QIODevice::ReadWrite))
{
//qDebug()<<tr("数据库:%1创建失败").arg(m_DBName);
getLogMsg()->iLogMsg(tr("数据库:%1创建失败").arg(m_DBName));
return false;
}
else
{
file.close();
m_SqlDB = QSqlDatabase::addDatabase("QSQLITE","UserDB");
m_SqlDB.setDatabaseName(m_DBName);
if(!m_SqlDB.open())
{
//qDebug()<<"数据库打开失败:"<<m_SqlDB.lastError().text();
getLogMsg()->iLogMsg(tr("数据库打开失败:%1").arg(m_SqlDB.lastError().text()));
return false;
}
if(!createTable(m_SqlDB))
{
return false;
}
}
}
else
{
m_SqlDB = QSqlDatabase::addDatabase("QSQLITE","UserDB");
m_SqlDB.setDatabaseName(m_DBName);
if(!m_SqlDB.open())
{
qDebug()<<"数据库打开失败:"<<m_SqlDB.lastError().text();
getLogMsg()->iLogMsg(tr("数据库打开失败:%1").arg(m_SqlDB.lastError().text()));
return false;
}
}
}
void MyDatebase::CloseDB()
{
m_SqlDB.close();
}
int MyDatebase::GetUserCount()
{
QSqlQuery query(m_SqlDB);
bool ret = query.exec("select count(*) from T_USER");
while(query.next())
{
return query.value(0).toInt();
}
return 0;
}
bool MyDatebase::CheckUser(QString UserNo, QString UserKey)
{
QSqlQuery query(m_SqlDB);
QByteArray bb;
bb = QCryptographicHash::hash(UserKey.toAscii(),QCryptographicHash::Md5);
query.exec(tr("select count(*) from T_USER where UserNO = '%1' and UserKey = '%2'").arg(UserNo).arg(bb.data()));
while(query.next())
{
if(query.value(0).toInt() >= 1)
{
return true;
}
}
return false;
}
bool MyDatebase::CreateUser(QString UserNo, QString UserKey)
{
QSqlQuery query(m_SqlDB);
query.exec(tr("select count(*) from T_USER where UserNO = '%1'").arg(UserNo));
while(query.next())
{
if(query.value(0).toInt() >= 1)
{
return false;
}
}
QByteArray bb;
bb = QCryptographicHash::hash(UserKey.toAscii(),QCryptographicHash::Md5);
if(query.exec(tr("insert into T_USER(UserNo,UserKey) values ('%1','%2')").arg(UserNo).arg(bb.data())))
{
return true;
}
return false;
}
bool MyDatebase::createTable(QSqlDatabase db)
{
QSqlQuery query(m_SqlDB);
bool ret = query.exec(CREATE_USER_TABLE);
if(!ret)
{
//qDebug()<<"创建表失败"<<query.lastError();
getLogMsg()->iLogMsg(tr("创建管理员用户表失败:").arg(query.lastError().text()));
return false;
}
// ret = query.exec("insert into T_USER(UserNo,UserKey) values ('autoveh','autoveh')");
// if(!ret)
// {
// qDebug()<<"创建原始管理员失败"<<query.lastError();
// getLogMsg()->iLogMsg(tr("创建管理员账号失败").arg(query.lastError().text()));
// return false;
// }
return true;
}
MyDatebase* getDB()
{
static MyDatebase* db = NULL;
if(db == NULL)
{
db = new MyDatebase();
}
return db;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fujingyuan/AutoVehTypeMon.git
git@gitee.com:fujingyuan/AutoVehTypeMon.git
fujingyuan
AutoVehTypeMon
AutoVehTypeMon
master

搜索帮助