连接服务器 这个很简单
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName(QString("DRIVER={SQL SERVER};"
"SERVER=%1;"
"DATABASE=%2;"
"UID=%3;"
"PWD=%4;")
.arg(Sever)
.arg(DB)
.arg(UID)
.arg(PWD));
/*.arg("192.168.2.21")
.arg("LabTest")
.arg("sa")
.arg("123456"));*/
if (!db.open())
{
qDebug()<<"Cannot open database!";
return false;
}
else
{
qDebug()<<"Connect to Database Success!";
return true;
}
增加,搜索以及删除
//这个使用了模糊搜索以及条件搜索 最后添加到QStringList后发送给界面
void QT_SDK::download_para(QString mystr)
{
QSqlQuery query;
"select * from PE_Valve_TaskParameter where operator_num='%1' and password ='%2'";
QString str1 = QString("select * from PE_Valve_TaskParameter where ProcessNum like '%%1%' and EquipmentNum = 'YC-SC-ZP-998' and UseState = ").arg(mystr);
QString str = QString::fromLocal8Bit("'可用'");
str1 += str;
qDebug() << str1 << endl;
query.exec(str1);
while(query.next()) //遍历数据表 table1
{
QSqlRecord rec = query.record();
qDebug() << rec.count() << endl;
QStringList str_List;
for(int i=0;i<rec.count();++i)//将每行的每一列结果得出来
{
QString strResult = query.value(i).toString();//表第i个字段的内容
str_List << strResult;
qDebug() << strResult << endl;
}
emit emit_tableList(str_List);
}
}
//上传 每次上传前判断网络是否通
void QT_SDK::upload_para(QStringList myList)
{
if(pingOk("192.168.2.21") == true)
{
QString str = QString("insert into PE_Valve_TaskParameter(ProductType,ProcessNum,EquipmentNum,UseState,VersionTime,BuildOperatorNum,BuildTime,AnnulOperatorNum,AnnulTime,NetConnectState,ValveType,Exposure1,Threshold1,Exposure2,Threshold2,Exposure3,Threshold3,Exposure4,Threshold4,Exposure5,Threshold5,Exposure6,Threshold6,Remarks) values('%1',%2,'%3','%4','%5',%6,'%7',%8,'%9','%10',%11,%12,%13,%14,%15,%16,%17,%18,%19,%20,%21,%22,%23,'%24')")
.arg(myList.at(1))
.arg(myList.at(2))
.arg(myList.at(3))
.arg(myList.at(4))
.arg(myList.at(5))
.arg(myList.at(6))
.arg(myList.at(7))
.arg(myList.at(8))
.arg(myList.at(9))
.arg(myList.at(10))
.arg(myList.at(11))
.arg(myList.at(12))
.arg(myList.at(13))
.arg(myList.at(14))
.arg(myList.at(15))
.arg(myList.at(16))
.arg(myList.at(17))
.arg(myList.at(18))
.arg(myList.at(19))
.arg(myList.at(20))
.arg(myList.at(21))
.arg(myList.at(22))
.arg(myList.at(23))
.arg(myList.at(24));
qDebug() << str << endl;
QSqlQuery query;
query.exec(str);
qDebug() << "insert success" << endl;
}
else
{
QString str = QString("insert into PE_Valve_TaskParameter(ProductType,ProcessNum,EquipmentNum,UseState,VersionTime,BuildOperatorNum,BuildTime,AnnulOperatorNum,AnnulTime,NetConnectState,ValveType,Exposure1,Threshold1,Exposure2,Threshold2,Exposure3,Threshold3,Exposure4,Threshold4,Exposure5,Threshold5,Exposure6,Threshold6,Remarks) values('%1',%2,'%3','%4','%5',%6,'%7',%8,'%9','%10',%11,%12,%13,%14,%15,%16,%17,%18,%19,%20,%21,%22,%23,'%24')")
.arg(myList.at(1))
.arg(myList.at(2))
.arg(myList.at(3))
.arg(myList.at(4))
.arg(myList.at(5))
.arg(myList.at(6))
.arg(myList.at(7))
.arg(myList.at(8))
.arg(myList.at(9))
.arg(myList.at(10))
.arg(myList.at(11))
.arg(myList.at(12))
.arg(myList.at(13))
.arg(myList.at(14))
.arg(myList.at(15))
.arg(myList.at(16))
.arg(myList.at(17))
.arg(myList.at(18))
.arg(myList.at(19))
.arg(myList.at(20))
.arg(myList.at(21))
.arg(myList.at(22))
.arg(myList.at(23))
.arg(myList.at(24));
qDebug() << str << endl;
QSqlQuery query;
query.exec(str);
qDebug() << "insert success" << endl;
}
}
//条件删除
void QT_SDK::delete_para(QString process_name)
{
QSqlQuery query;
QString str = QString("delete from PE_Valve_TaskParameter where ProcessNum = '%1'").arg(process_name);
query.exec(str);//删除信息
}
其实不用写sql语句也能实现数据库的增删改 注意不要更改自增的数据类型
不写sql语句 使用QSqlTableModel模块也可以实现
创建好一个tableView 照着上面连接好数据库 然后输入下面代码 整个表格就打到tabelView里面去了
model = new QSqlTableModel(this);
model->setTable("PE_Valve_TaskParameter");
model->select();
// 设置编辑策略
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
ui->tableView->setModel(model);
QString name = "1";
if(name.length())
{
//根据姓名进行筛选, 一定要使用单引号
model->setFilter(QString("ID = '%1'").arg(name));
model->select();
}