根据Sqlite轻量级数据库的优势,便于对于数据进行增删改查等操作。
本例模拟权限列表对不同界面进行权限管理,管理员针对不同人员进行权限设定,灵活性更高。
动态演示效果如下:
考虑数据库数据安全,可进行哈希散列值计算进行加密,Qt提供QCryptographicHash类进行数据加密,本例中使用的加密函数为:
QString SqlHelper::encode(QString str) {
QString strPwdSha = "";
if (str == "") {
return "";
}
QByteArray bytePwd = str.toLatin1();
QByteArray bytePwdSha = QCryptographicHash::hash(bytePwd, QCryptographicHash::Sha256);
strPwdSha = bytePwdSha.toHex().toUpper();
return strPwdSha;
}
QString SqlHelper::encodePwd(QString str) {
if (str == "") {
return "";
}
QString endata = encode(str);
QString strdata = "";
for (int i = 0; i < endata.size(); i++) {
if (i % 4 == 0) {
strdata += endata[i];
}
}
return strdata + "SAP";
}
初始化数据库:
void SqlHelper::connectDataBase() {
db = QSqlDatabase::addDatabase("QSQLITE", "sqlite1");// 驱动类型,连接名(默认)
db.setDatabaseName("DbData/data.db");
db.setHostName("localhost.");
db.setUserName("root");
db.setPassword("123456");
m_DbState = db.open();
}
创建数据表:
void SqlHelper::IniSql() {
QSqlQuery query(db);
QString strCmd = QString("create table employee(employee_id varchar(64) primary key,"
"employee_name varchar(64),employee_pass varchar(32),employee_settingrole varchar(16),"
"employee_menurole varchar(16),employee_logrole varchar(16),employee_sex varchar(16))");
query.prepare(strCmd);
if (query.exec())
{
qDebug() << "----employee 数据库表:创建成功!";
insertData("admin", "root", "root123456", 1,1,1,"male");
}
else {
qDebug() << "----employee 数据表已经存在-----";
}
}
插入数据:
bool SqlHelper::insertData(QString userid, QString username, QString userpass, int sflag, int mflag, int lflag, QString usersex) {
if (!m_DbState) {
return false;
}
QList list = queryAllInfo();
m_mutex.lock();
bool flag = false;
//qDebug() << "m_id: " << m_id;
QSqlQuery query(db);
QString str = QString("insert into employee values(?,?,?,?,?,?,?)");
query.prepare(str);
//query.bindValue(0, list.size() + 1);
query.bindValue(0, userid);
query.bindValue(1, username);
QString pass = encodePwd(userpass);
query.bindValue(2, pass);
QString setrole = userid;
setrole.append(QString::number(sflag));
setrole = encodeRole(setrole,RoleType::SettingRole);
query.bindValue(3, setrole);
QString mrole = userid;
mrole.append(QString::number(mflag));
mrole = encodeRole(mrole, RoleType::MenuRole);
query.bindValue(4, mrole);
QString lrole = userid;
lrole.append(QString::number(lflag));
lrole = encodeRole(lrole, RoleType::LogRole);
query.bindValue(5, lrole);
query.bindValue(6, usersex);
if (query.exec()) {
qDebug() << "插入数据成功----";
flag = true;
}
else {
qDebug() << "插入数据失败----";
}
m_mutex.unlock();
//qDebug() << "777777777777777777777777";
return flag;
}
可使用结构体对数据信息进行封装:
struct employeeInfo {
QString employee_id = "";
QString employee_name;
QString employee_pass;
QString employee_settingrole;
QString employee_menurole;
QString employee_logrole;
QString employee_sex;
};
数据库查询函数为:
QList SqlHelper::queryAllInfo() {
QList list;
if (!m_DbState) {
return list;
}
m_mutex.lock();
QSqlQuery query(db);
QString str = "select * from employee";
query.prepare(str);
if (query.exec()) {
while (query.next()) {
//qDebug() << "name---" << query.value(1).toString() << " age-" << query.value(2).toInt();
employeeInfo employee;
employee.employee_id = query.value(0).toString();
employee.employee_name = query.value(1).toString();
employee.employee_pass = query.value(2).toString();
employee.employee_settingrole = query.value(3).toString();
employee.employee_menurole = query.value(4).toString();
employee.employee_logrole = query.value(5).toString();
employee.employee_sex = query.value(6).toString();
list.append(employee);
}
}
m_mutex.unlock();
return list;
}
employeeInfo SqlHelper::queryOne(QString userid) {
employeeInfo employee;
if (!m_DbState) {
return employee;
}
m_mutex.lock();
QSqlQuery query(db);
QString str = "select * from employee where employee_id =?";
query.prepare(str);
query.addBindValue(userid);
if (query.exec()) {
while (query.next()) {
employee.employee_id = query.value(0).toString();
employee.employee_name = query.value(1).toString();
employee.employee_pass = query.value(2).toString();
employee.employee_settingrole = query.value(3).toString();
employee.employee_menurole = query.value(4).toString();
employee.employee_logrole = query.value(5).toString();
employee.employee_sex = query.value(6).toString();
}
}
m_mutex.unlock();
return employee;
}
数据库更新数据函数为:
bool SqlHelper::updateData(QString userid, QString username, QString userpass, int sflag ,int mflag, int lflag, QString usersex) {
if (!m_DbState) {
return false;
}
QSqlQuery query(db);
QString str = "update employee set employee_name =?,employee_pass=?,"
"employee_settingrole=?,employee_menurole=?,employee_logrole=? , employee_sex = ? where employee_id =?";
query.prepare(str);
query.addBindValue(username);
QString ss = queryPass(userid);
m_mutex.lock();
bool flag = false;
if (ss == userpass) {
query.addBindValue(ss);
}
else {
userpass = encodePwd(userpass);
query.addBindValue(userpass);
}
QString nsr = userid;
nsr.append(QString::number(sflag));
nsr = encodeRole(nsr, RoleType::SettingRole);
query.addBindValue(nsr);
QString nmr = userid;
nmr.append(QString::number(mflag));
nmr = encodeRole(nmr, RoleType::MenuRole);
query.addBindValue(nmr);
QString nlr = userid;
nlr.append(QString::number(lflag));
nlr = encodeRole(nlr, RoleType::LogRole);
query.addBindValue(nlr);
query.addBindValue(usersex);
query.addBindValue(userid);
if (query.exec()) {
flag = true;
qDebug() << "更新成功---";
}
else {
flag = false;
qDebug() << "更新失败---";
}
m_mutex.unlock();
return flag;
}
删除数据函数为:
bool SqlHelper::deleteData(QString userid) {
if (!m_DbState) {
return false;
}
employeeInfo one = queryOne(userid);
if (one.employee_id == "") {
qDebug() << "此人已删除----";
return false;
}
m_mutex.lock();
bool flag = false;
QSqlQuery query(db);
QString str = "delete from employee where employee_id=?";
query.prepare(str);
query.addBindValue(userid);
if (query.exec()) {
flag = true;
qDebug() << "删除成功---";
}
else {
flag = false;
qDebug() << "删除失败---";
}
m_mutex.unlock();
return flag;
}