#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableWidget->clearContents();
ui->tableWidget->setColumnCount(3);
ui->tableWidget->setHorizontalHeaderLabels(QStringList() << QString::fromLocal8Bit("学 号") << QString::fromLocal8Bit("姓名") << QString::fromLocal8Bit("分数"));
ui->tableWidget->takeVerticalHeaderItem(0);
ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //先自适应宽度
ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents); //然后设置要根据内容使用宽度的列
ui->tableWidget->verticalHeader()->hide();
CreateDatabaseFunc();
CreateTableFunc();
}
MainWindow::~MainWindow()
{
delete ui;
}
//创建数据库
void MainWindow::CreateDatabaseFunc()
{
sqldb = QSqlDatabase::addDatabase("QSQLITE");
sqldb.setDatabaseName("studentmis.db");
if(sqldb.open())
{
QMessageBox::information(this, "ok", "open sucess", QMessageBox::Ok);
}
else {
QMessageBox::information(this, "ok", "open failed", QMessageBox::Ok);
}
}
//创建数据表
void MainWindow::CreateTableFunc()
{
QSqlQuery createquery;
QString strsql = QString("create table if not exists student ("
"id int primary key not null,"
"name text not null,"
"score real not null)");
if(createquery.exec(strsql))
{
QMessageBox::information(this, "ok", "create table sucess", QMessageBox::Ok);
}
else {
QMessageBox::information(this, "ok", "create table failed", QMessageBox::Ok);
}
}
//执行查询
void MainWindow::QueryTableFunc()
{
}
void MainWindow::on_pushButton_SORT_clicked()
{
}
void MainWindow::on_pushButton_INSERT_clicked()
{
QSqlQuery sqlquery;
int id = ui->lineEdit_ID->text().toInt();
QString name = ui->lineEdit_NAME->text();
double score = ui->lineEdit_SCORE->text().toDouble();
QString strs = QString("insert into student "
"values(%1,'%2',%3)").arg(id).arg(name).arg(score);
if(sqlquery.exec(strs))
{
QMessageBox::information(this, "ok", "insert data sucess", QMessageBox::Ok);
}
else{
QMessageBox::information(this, "ok", "create table failed", QMessageBox::Ok);
}
}
void MainWindow::on_pushButton_DELETE_clicked()
{
QSqlQuery sqlquery;
QString name = ui->lineEdit_NAME->text();
QString strs = QString("delete from student where values = %1").arg(name);
if(sqlquery.exec(strs))
{
QMessageBox::information(this, "ok", "insert data sucess", QMessageBox::Ok);
}
else{
QMessageBox::information(this, "ok", "create table failed", QMessageBox::Ok);
}
}
void MainWindow::on_pushButton_UPDATE_clicked()
{
QSqlQuery sqlquery;
QString name = ui->lineEdit_NAME->text();
QString strs = QString("update student set name = 'zhangsan' where id = 2");
if(sqlquery.exec(strs))
{
QMessageBox::information(this, "ok", "insert data sucess", QMessageBox::Ok);
}
else{
QMessageBox::information(this, "ok", "create table failed", QMessageBox::Ok);
}
}
void MainWindow::on_pushButton_SEARCH_clicked()
{
QSqlQuery sqlquery;
QString strs = QString("select * from student ");
ui->tableWidget->clearContents();
//设置表格数据区内的所有单元格都不允许编辑
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
//设置表格中每一行
if(sqlquery.exec(strs))
{
int nCount = 0;
ui->tableWidget->setRowCount(nCount+1);
while (sqlquery.next()) {
int id = sqlquery.value(0).toInt();
QString strName = sqlquery.value(1).toString();
double dScore = sqlquery.value(2).toDouble();
ui->tableWidget->setItem(nCount,0,new QTableWidgetItem(QString::number(id)));
ui->tableWidget->setItem(nCount,1,new QTableWidgetItem(strName));
ui->tableWidget->setItem(nCount,2,new QTableWidgetItem(QString::number(dScore)));
nCount++;
ui->tableWidget->setRowCount(nCount+1);
}
}
else{
QMessageBox::information(this, "ok", "create table failed", QMessageBox::Ok);
}
update();
}

牵牛老人
- 粉丝: 2371
最新资源
- HP工作站BIOS详解.doc
- 浅谈通信工程项目的团队建设(1).docx
- 电路的计算机仿真(1).ppt
- 基于信息化教学的初中语文教学改革研究(1).docx
- 互联网金融商业综合计划书.docx
- 海尔集团网络专项方案.doc
- 财大软件公司网络营销计划(1).pptx
- 计算机专业毕业设计方案.doc
- 操作系统试验参考指导书完整版.doc
- PLSQL基本操作基础手册.doc
- 互联网金融发展中的风险防控策略探讨(1).docx
- 基于单片机的温湿度控制基础系统综合设计.docx
- SDCN网络安全防火墙部分重点技术基础规范.docx
- genesis软件操作教程.ppt
- VB课设销售基础管理系统.docx
- 图书管理系统详细设计说明书(1).doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


