主要是从本人的项目中抠出来的,便于记录:

.cpp

#include "protuctmysql.h"



QProtuctMySQL::QProtuctMySQL()


{


isopen = false;
databasehost = "localhost";     //主机
strUserID = "root";             //用户
databasename = "protuctmanage"; //数据库
strPassWord = "123456";         //连接密码
conname = "con1";               //连接名


}



QProtuctMySQL::~QProtuctMySQL()


{



}



//打开数据库
void QProtuctMySQL::openDatabase()


{


if(QSqlDatabase::contains("con1"))
QSqlDatabase::removeDatabase("con1");



db = QSqlDatabase::addDatabase("QODBC",conname);
db.setHostName(databasehost);
db.setDatabaseName(databasename);
db.setUserName(strUserID);
db.setPassword(strPassWord);



isopen = db.open();
if(!isopen)
{
qDebug() << "数据库打开失败";
return;
}


}



//关闭数据库
void QProtuctMySQL::closeDatabase()


{


db.close();
isopen=false;


}



//根据账号查询用户表
QStringList QProtuctMySQL::queryacc(QString account)


{


QStringList retstr;
QString sq=QStringLiteral("select *from manage_power where 账号='%1' ").arg(account);



sqlquery=QSqlQuery(db);
sqlquery.exec(sq);
while (sqlquery.next())
{
for(int i = 1; i < 8; i++)
retstr.append(sqlquery.value(i).toString());
}



return retstr;


}



//创建数据表,参数namelist为属性名
bool QProtuctMySQL::creatProTable(QStringList namelist)


{


QString qslname;



for(int i = 1; i < namelist.count() - 1; i++)
qslname+=(namelist.at(i)+" VARCHAR(40),");



sqlquery=QSqlQuery(db);
QString sq=QString("create table %1(" "id INT NOT NULL AUTO_INCREMENT,%2PRIMARY KEY (id));")
.arg(namelist.at(0)).arg(qslname);



return sqlquery.exec(sq);


}



//根据id修改数据表的数据
bool QProtuctMySQL::updateProductMessage(QStringList qstrl1, QStringList qstrl2, int id, QString tablename)


{


QString sqlset;



sqlquery = QSqlQuery(db);
for(int i = 0; i < qstrl1.count(); i++)
sqlset+=""+qstrl1.at(i)+" ='"+qstrl2.at(i)+"',";



sqlset = sqlset.left(sqlset.length() - 1);



QString sq = QString("update %2 set " + sqlset + " where id=%1").arg(id).arg(tablename);



return sqlquery.exec(sq);


}



//插入数据到数据表
bool QProtuctMySQL::insertProductnum(QStringList qstrl1, QStringList qstrl2, QString tablename)


{


sqlquery=QSqlQuery(db);



QString sqlset;
for(int i = 0; i < qstrl1.count(); i++)
sqlset += "" + qstrl1.at(i) + " ='" + qstrl2.at(i) + "',";
sqlset = sqlset.left(sqlset.length() - 1);



QString sq=QString("insert %1 set "+sqlset+" ").arg(tablename);



return sqlquery.exec(sq);


}



//向数据表添加列
bool QProtuctMySQL::addtablecol(QString str, QString tablename)


{


sqlquery=QSqlQuery(db);



QString sq=QString("ALTER TABLE %2 add %1 VARCHAR(50) ").arg(str).arg(tablename);



return sqlquery.exec(sq);


}



//在数据表删除列
bool QProtuctMySQL::deltablecol(QString str, QString tablename)


{


sqlquery = QSqlQuery(db);



QString sq = QString("ALTER TABLE %2 drop %1").arg(str).arg(tablename);



return sqlquery.exec(sq);


}



//遍历数据表
QJsonArray QProtuctMySQL::printProductMessage(QString tablename, QString mingcheng)


{


QStringList qsl;
QJsonArray qja;
QJsonObject obj;



QString feildname = QStringLiteral("名称");
QString sq1 = QString("show columns from %1  ").arg(tablename);     //获取字段名
QString sq = QString("select *from %1  ").arg(tablename);           //查询电缆表的数据
if(mingcheng != "")
sq=QString("select *from %1 where %2 like '%%3%' ").arg(tablename).arg(feildname).arg(mingcheng);



sqlquery=QSqlQuery(db);
sqlquery.exec(sq1);
while (sqlquery.next())
{
qsl.append(sqlquery.value(0).toString());
obj.insert(sqlquery.value(0).toString(),sqlquery.value(0).toString());
}



qja.append(obj);
sqlquery.exec(sq);
while (sqlquery.next())
{
QJsonObject obj1;
for(int i=0;i<qsl.length();i++)
obj1.insert(qsl.at(i),sqlquery.value(i).toString());



qja.append(obj1);
}



return qja;


}



//查询
QJsonArray QProtuctMySQL::searchModelProductMessage(QString tablename, QString model)


{


QString feildname=QStringLiteral("型号");
QString sq1=QString("show columns from %1  ").arg(tablename);//获取字段名
QString sq=QString("select *from %1  ").arg(tablename);//查询电缆表的数据
if(model != ""){
sq=QString("select *from %1 where %2 like '%%3%' ").arg(tablename).arg(feildname).arg(model);
}
sqlquery=QSqlQuery(db);
sqlquery.exec(sq1);



QStringList qsl;
QJsonArray qja;
QJsonObject obj;



while (sqlquery.next()) {
qsl.append(sqlquery.value(0).toString());
obj.insert(sqlquery.value(0).toString(),sqlquery.value(0).toString());
}
qja.append(obj);
sqlquery.exec(sq);
while (sqlquery.next()) {
QJsonObject obj1;
for(int i=0;i<qsl.length();i++){
obj1.insert(qsl.at(i),sqlquery.value(i).toString());
}
qja.append(obj1);
}



return qja;


}



QJsonArray QProtuctMySQL::searchSupplyProductMessage(QString tablename, QString supply)


{


QString feildname=QStringLiteral("供应商");
QString sq1=QString("show columns from %1  ").arg(tablename);//获取字段名
QString sq=QString("select *from %1  ").arg(tablename);//查询电缆表的数据
if(supply != ""){
sq=QString("select *from %1 where %2 like '%%3%' ").arg(tablename).arg(feildname).arg(supply);
}
sqlquery=QSqlQuery(db);
sqlquery.exec(sq1);



QStringList qsl;
QJsonArray qja;
QJsonObject obj;



while (sqlquery.next()) {
qsl.append(sqlquery.value(0).toString());
obj.insert(sqlquery.value(0).toString(),sqlquery.value(0).toString());
}
qja.append(obj);
sqlquery.exec(sq);
while (sqlquery.next()) {
QJsonObject obj1;
for(int i=0;i<qsl.length();i++){
obj1.insert(qsl.at(i),sqlquery.value(i).toString());
}
qja.append(obj1);
}



return qja;


}



//根据id删除数据
bool QProtuctMySQL::deleteProductat(int id, QString tablename)


{


sqlquery=QSqlQuery(db);



QString sq=QString("delete from %2 where id=%1").arg(id).arg(tablename);



return sqlquery.exec(sq);


}



bool QProtuctMySQL::deleteProductAboutName(QString tablename, QString commodityName)


{


sqlquery=QSqlQuery(db);



QString feild = QStringLiteral("名称");
QString sq=QString("delete from %1 where %2 like '%%3%'").arg(tablename).arg(feild).arg(commodityName);



return sqlquery.exec(sq);


}



QJsonArray QProtuctMySQL::printPersonMessage(QString feild,QString feildvalue)


{


QStringList qsl;
QJsonArray qja;
QJsonObject obj;



QString sq1=QString("show columns from table_personnel  ");//获取字段名
QString sq=QString("select *from table_personnel where %1 like '%%2%' ")
.arg(feild).arg(feildvalue);        //查询人员表的数据



sqlquery=QSqlQuery(db);
sqlquery.exec(sq1);
while (sqlquery.next())
{
qsl.append(sqlquery.value(0).toString());
obj.insert(sqlquery.value(0).toString(),sqlquery.value(0).toString());
}



qja.append(obj);
sqlquery.exec(sq);
while (sqlquery.next())
{
QJsonObject obj1;
for(int i=0;i<qsl.length();i++){
obj1.insert(qsl.at(i),sqlquery.value(i).toString());}
qja.append(obj1);
}



return qja;


}



QJsonArray QProtuctMySQL::printAccountMana(QString feildname)


{


QStringList qsl;
QJsonArray qja;
QJsonObject obj;



QString feild = QStringLiteral("姓名");
QString sq1=QString("show columns from manage_power  ");//获取字段名
QString sq=QString("select *from manage_power where %1 like '%%2%' ")
.arg(feild).arg(feildname);      //查询人员表的数据



sqlquery=QSqlQuery(db);
sqlquery.exec(sq1);
while (sqlquery.next())
{
qsl.append(sqlquery.value(0).toString());
obj.insert(sqlquery.value(0).toString(),sqlquery.value(0).toString());
}



qja.append(obj);
sqlquery.exec(sq);
while (sqlquery.next())
{
QJsonObject obj1;
for(int i=0;i<qsl.length();i++)
obj1.insert(qsl.at(i),sqlquery.value(i).toString());



qja.append(obj1);
}



return qja;


}



QJsonArray QProtuctMySQL::printAccountManaAboutNum(QString number)


{


QStringList qsl;
QJsonArray qja;
QJsonObject obj;



QString feild = QStringLiteral("编号");
QString sq1=QString("show columns from manage_power  ");//获取字段名
QString sq=QString("select *from manage_power where %1 like '%%2%' ")
.arg(feild).arg(number);      //查询人员表的数据



sqlquery=QSqlQuery(db);
sqlquery.exec(sq1);
while (sqlquery.next())
{
qsl.append(sqlquery.value(0).toString());
obj.insert(sqlquery.value(0).toString(),sqlquery.value(0).toString());
}



qja.append(obj);
sqlquery.exec(sq);
while (sqlquery.next())
{
QJsonObject obj1;
for(int i=0;i<qsl.length();i++)
obj1.insert(qsl.at(i),sqlquery.value(i).toString());



qja.append(obj1);
}



return qja;


}



QJsonArray QProtuctMySQL::printAccountManaAboutAcc(QString account)


{


QString sq1=QString("show columns from manage_power  ");//获取字段名
QString sq=QString("select *from manage_power where %1='%2' ")
.arg(QStringLiteral("账号")).arg(account);//查询人员表的数据



sqlquery=QSqlQuery(db);
sqlquery.exec(sq1);



QStringList qsl;
QJsonArray qja;



QJsonObject obj;



while (sqlquery.next()) {
qsl.append(sqlquery.value(0).toString());
obj.insert(sqlquery.value(0).toString(),sqlquery.value(0).toString());
}
qja.append(obj);
sqlquery.exec(sq);
while (sqlquery.next()) {
QJsonObject obj1;
for(int i=0;i<qsl.length();i++){
obj1.insert(qsl.at(i),sqlquery.value(i).toString());}
qja.append(obj1);
}
return qja;


}



bool QProtuctMySQL::updatePassWord(QString account, QString password)


{


sqlquery=QSqlQuery(db);



QString sq=QString("update manage_power set %1="+password+" where %2=" + account)
.arg(QStringLiteral("密码")).arg(QStringLiteral("账号"));



return sqlquery.exec(sq);


}



bool QProtuctMySQL::insertPersontnum(QStringList qstrl1, QStringList qstrl2)


{


sqlquery=QSqlQuery(db);



QString sqlset;
for(int i=0;i<qstrl1.count();i++)
sqlset+=""+qstrl1.at(i)+" ='"+qstrl2.at(i)+"',";
sqlset = sqlset.left(sqlset.length()-1);
QString sq = QString("select count(*) from table_personnel where %1='%2' ")
.arg(qstrl1.at(1)).arg(qstrl2.at(1)) ;



sqlquery.exec(sq);
while (sqlquery.next()){
if(sqlquery.value(0)!=0)
return false;
}



sq=QString("insert table_personnel set "+sqlset+" ");



return sqlquery.exec(sq);


}



bool QProtuctMySQL::insertManager(QStringList qstrl1, QStringList qstrl2)


{


QString sqlset;



sqlquery=QSqlQuery(db);



sqlset += "" + qstrl1.first() + " ='" + qstrl2.first() + "',";
qstrl1.removeFirst();
qstrl2.removeFirst();
sqlset += "" + qstrl1.first() + " ='" + qstrl2.first() + "',";
for(int i=1;i<qstrl1.count()-1;i++)
sqlset += "" + qstrl1.at(i) + " =" + qstrl2.at(i) + ",";
sqlset += "" + qstrl1.last() + " ='" + qstrl2.last() + "'";



QString sq=QString("insert manage_power set "+sqlset+" ");



return sqlquery.exec(sq);


}



bool QProtuctMySQL::updatePersontMessage(QStringList qstrl1, QStringList qstrl2, int id)


{


QString sqlset;



sqlquery=QSqlQuery(db);



for(int i = 0; i < qstrl1.count(); i++)
sqlset += "" + qstrl1.at(i) + " ='" + qstrl2.at(i) + "',";
sqlset = sqlset.left(sqlset.length() - 1);



QString sq=QString("update table_personnel set "+sqlset+" where id=%1").arg(id);



return sqlquery.exec(sq);


}



bool QProtuctMySQL::updateAccountMessage(QStringList qstrl1, QStringList qstrl2, int id)


{


sqlquery=QSqlQuery(db);
QString sqlset;
sqlset+=""+qstrl1.first()+" ='"+qstrl2.first()+"',";
qstrl1.removeFirst();qstrl2.removeFirst();
sqlset+=""+qstrl1.first()+" ='"+qstrl2.first()+"',";
qstrl1.removeFirst();qstrl2.removeFirst();
sqlset+=""+qstrl1.first()+" ='"+qstrl2.first()+"',";
for(int i=1;i<qstrl1.count()-1;i++){
sqlset+=""+qstrl1.at(i)+" ="+qstrl2.at(i)+",";
}
sqlset+=""+qstrl1.last()+" ='"+qstrl2.last()+"'";
QString sq=QString("update manage_power set "+sqlset+" where id=%1").arg(id);



return sqlquery.exec(sq);


}



bool QProtuctMySQL::deletePersonat(int id)


{


sqlquery=QSqlQuery(db);



QString sq=QString("delete from table_personnel where id=%1").arg(id);



return sqlquery.exec(sq);


}



bool QProtuctMySQL::deleteAccountat(int id)


{


sqlquery = QSqlQuery(db);



QString sq = QString("delete from manage_power where id=%1").arg(id);



return sqlquery.exec(sq);


}



bool QProtuctMySQL::deletePersonNumber(QString number)


{


sqlquery=QSqlQuery(db);



QString sq=QString("delete from table_personnel where %2=%1").arg(number).arg(QStringLiteral("编号"));



return sqlquery.exec(sq);


}



bool QProtuctMySQL::deleteAccountnumber(QString number)


{


sqlquery=QSqlQuery(db);
QString sq=QString("delete from manage_power where %2='%1'").arg(number).arg(QStringLiteral("编号"));
bool retb=sqlquery.exec(sq);
return retb;


}



QStringList QProtuctMySQL::getTables()


{


QString sq = QString("show tables");
QStringList retstrl;



sqlquery = QSqlQuery(db);
sqlquery.exec(sq);
while(sqlquery.next())
retstrl.append( sqlquery.value(0).toString());



return retstrl;


}



QJsonArray QProtuctMySQL::showDutiesTable(QString feildname)


{


QString sq1=QString("show columns from %1  ").arg(QStringLiteral("职能表")) ;//获取字段名
QString sq=QString("select *from %3 where %1 like '%2%' ")
.arg(QStringLiteral("名称")).arg(feildname)
.arg(QStringLiteral("职能表"));//查询职能表的数据
sqlquery=QSqlQuery(db);
sqlquery.exec(sq1);



QStringList qsl;
QJsonArray qja;



QJsonObject obj;



while (sqlquery.next()) {
qsl.append(sqlquery.value(0).toString());
obj.insert(sqlquery.value(0).toString(),sqlquery.value(0).toString());
}
qja.append(obj);
sqlquery.exec(sq);
while (sqlquery.next()) {
QJsonObject obj1;
for(int i=0;i<qsl.length();i++){
obj1.insert(qsl.at(i),sqlquery.value(i).toString());}
qja.append(obj1);
}
return qja;


}



bool QProtuctMySQL::insertDuty(QStringList qstrl1, QStringList qstrl2)


{


sqlquery=QSqlQuery(db);
QString sqlset;
for(int i=0;i<qstrl1.count()-1;i++){
sqlset+=""+qstrl1.at(i)+" ='"+qstrl2.at(i)+"',";
}
sqlset+=""+qstrl1.last()+" ='"+qstrl2.last()+"'";
QString sq=QString("insert %1 set "+sqlset+" ").arg(QStringLiteral("职能表"));
bool retb=sqlquery.exec(sq);
return retb;


}



bool QProtuctMySQL::updateDuty(QStringList qstrl1, QStringList qstrl2, int id)


{


sqlquery=QSqlQuery(db);
QString sqlset;
for(int i=1;i<qstrl1.count()-1;i++){
sqlset+=""+qstrl1.at(i)+" ='"+qstrl2.at(i)+"',";
}
sqlset+=""+qstrl1.last()+" ='"+qstrl2.last()+"'";
QString sq=QString("update %2 set "+sqlset+" where id=%1").arg(id).arg(QStringLiteral("职能表"));
bool retb=sqlquery.exec(sq);
return retb;


}



bool QProtuctMySQL::deleteDuty(int id)


{


sqlquery=QSqlQuery(db);
QString sq=QString("delete from %2 where id=%1").arg(id).arg(QStringLiteral("职能表")) ;
bool retb=sqlquery.exec(sq);
return retb;


}



.h



#include <QtSql>
#include <QSqlQuery>
#include <QJsonObject>
#include <QJsonArray>



class QProtuctMySQL


{


public:
explicit QProtuctMySQL();
~QProtuctMySQL();



public:
void openDatabase();                        //连接打开数据库
void closeDatabase();                       //关闭数据库



QStringList queryacc(QString account);      //账号密码查询
bool creatProTable(QStringList namelist);   //创建商品信息表



bool updateProductMessage(QStringList qstrl1,QStringList qstrl2,int id,QString tablename);  //更新商品数据
bool insertProductnum(QStringList qstrl1,QStringList qstrl2,QString tablename);             //插入商品信息
bool addtablecol(QString str,QString tablename);                                            //给商品表添加新列
bool deltablecol(QString str,QString tablename);                                            //给商品表删除列
QJsonArray printProductMessage(QString tablename, QString mingcheng);                       //遍历商品信息
QJsonArray searchModelProductMessage(QString tablename, QString model);                     //按型号搜索展示商品信息
QJsonArray searchSupplyProductMessage(QString tablename, QString supply);                   //按供应商展示商品信息
bool deleteProductat(int id,QString tablename);                                             //删除商品信息
bool deleteProductAboutName(QString tablename, QString commodityName);                      //按名称删除商品信息



QJsonArray printPersonMessage(QString feild, QString feildvalue);           //遍历人员信息
QJsonArray printAccountMana(QString feildname);                             //按姓名查找账号管理信息
QJsonArray printAccountManaAboutNum(QString number);                        //按编号查询账号管理信息
QJsonArray printAccountManaAboutAcc(QString account);                       //按账号查询账号管理信息
bool updatePassWord(QString account, QString password);                     //按账号修改员工密码
bool insertPersontnum(QStringList qstrl1,QStringList qstrl2);               //插入人员信息
bool insertManager(QStringList qstrl1,QStringList qstrl2);                  //插入管理人员信息
bool updatePersontMessage(QStringList qstrl1,QStringList qstrl2,int id);    //更改人员数据
bool updateAccountMessage(QStringList qstrl1,QStringList qstrl2,int id);    //更改管理人员数据
bool deletePersonat(int id);                                                //删除人员信息
bool deleteAccountat(int id);                                               //删除管理人员信息
bool deletePersonNumber(QString number);                                    //按编号删除人员信息
bool deleteAccountnumber(QString number);                                   //按编号删除管理人员信息
QStringList getTables();                                                    //获取表名
QJsonArray showDutiesTable(QString feildname);                              //职能表信息
bool insertDuty(QStringList qstrl1,QStringList qstrl2);                     //添加职能表信息
bool updateDuty(QStringList qstrl1,QStringList qstrl2,int id);              //更改职能表信息数据
bool deleteDuty(int id);                                                    //删除职能表信息



private:
bool isopen;                //是否已打开数据库



QSqlDatabase db;            //创建数据库连接及连接信息



QString databasehost;
QString strUserID;
QString strPassWord;
QString databasename;
QString conname;



QSqlQuery sqlquery;         //创建查询


};



调用:

QProtuctMySQL protuctMySQL;
    protuctMySQL.openDatabase();

//比如我要无条件查询“产品种类表”
QJsonArray jsonArray = protuctMySQL.printProductMessage(QStringLiteral("产品种类表"), "");
QStringList headList = jsonArray.at(0).toObject().keys();
QStringList tables;
jsonArray.removeFirst();            //first为表头,记忆中是这样的,(代码为后期扣的)
for(int i = 0; i < jsonArray.count(); i++)
{
QJsonObject jsonObject = jsonArray.at(i).toObject();
tables.append(jsonObject.value(headList.at(headList.indexOf(QStringLiteral("名称")))).toString());
}

 


以上代码中:jsonObject.value(headList.at(headList.indexOf(QStringLiteral("名称")))).toString()
是将jsonObject中的“名称”对应的值转换成字符串