void Widget::on_show_clicked()
{
QString sql = "select * from myTable" ;
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"失败","展示失败");
return;
}
//此时,将查找到的所有结果,全部都放在querry对象中
//可以通过next函数不断遍历查询结果
int i=0;
while(querry.next())
{
//遍历的就是任意一组记录,querry.rec
// qDebug()<<querry.record();
//将数据库中的表格展示到ui界面
// ui->tableWidget->setItem()
//querry.record().value(2).toString();
for(int j=0;j<querry.record().count()-1;j++)
{
// ui->tableWidget->setItem(i,j,new QTableWidgetItem());
ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}
i++;
}
}void Widget::on_xun_clicked()
{
QString sql = QString("SELECT * FROM myTable WHERE id=%1").arg(ui->xunedit->text().toInt());
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"失败","查找失败");
return;
}
deletelist();
int i=0;
while(querry.next())
{
for(int j=0;j<querry.record().count()-1;j++)
{
ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));
}
i++;
}}
void Widget::on_shan_clicked()
{
QString sql = QString("DELETE FROM myTable WHERE id=%1").arg(ui->shanedit->text().toInt());
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"失败","删除失败");
qDebug()<<querry.lastError().text();
}else
{
QMessageBox::information(this,"成功","删除数据成功");
deletelist();
}
}