#include "mainwindow.h"
#include "ui_mainwindow.h"
QString name[6] = {"李华","张小强","张伟","王芳","李秀英","郑小明"};
QString stuID[6] = {"202201","202202","202203","202204","202205","202206"};
QString address[6] = {"未央区","碑林区","雁塔区","长安区","鄠邑区","新城区"};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("SQL练习");
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//使用sqllite数据库
query = new QSqlQuery(db);
db.setHostName("rch"); //设计数据库主机名
db.setDatabaseName("qtDB.db");//创建数据库文件
db.setUserName("renchenhao");//数据库用户名
db.setPassword("123");//数据库密码
db.open(); //打开数据库
table1Modeldel = new QSqlTableModel(this, db);
table2Modeldel = new QSqlTableModel(this, db);
retModel = new QSqlQueryModel(this);
initSql();
displaySql();
//ui->tbView_sql->setSortingEnabled(true);//点击表头列排序
ui->tbView_table1->verticalHeader()->hide();
ui->tbView_table2->verticalHeader()->hide();
ui->tbView_ret->verticalHeader()->hide();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initSql()
{
initTable1();
initTable2();
}
void MainWindow::initTable1()
{
query->exec("DROP TABLE table1");//清空数据库表table1
//创建数据库表table1
bool ok = query->exec("create table table1(id int primary key,"
"name varchar,"
"score int,"
"subject varchar)");
if(ok)
qDebug()<<"表table1创建成功";
else
qDebug()<<"表table1创建失败";
query->exec("select * from table1");
QSqlRecord rec = query->record();
qDebug()<< "table1表字段数:"<< rec.count();
//插入记录
query->prepare("insert into table1 values(?,?,?,?)");
for(int i = 1; i <= 6; i++)
{
query->bindValue(0, i);
query->bindValue(1, name[i-1]);
query->bindValue(2, rand()%100);
query->bindValue(3, "语文");
ok = query->exec();
if(!ok){
QSqlError lasterror= query->lastError();
qDebug()<< lasterror.driverText()<< "table1数据插入失败";
}
}
}
void MainWindow::initTable2()
{
query->exec("DROP TABLE table2");//清空数据库表table2
//创建数据库表table2
bool ok = query->exec("create table table2(name varchar,"
"stuID varchar,"
"address varchar)");
if(ok)
qDebug()<<"表table2创建成功";
else
qDebug()<<"表table2创建失败";
query->exec("select * from table1");
QSqlRecord rec = query->record();
qDebug()<< "table1表字段数:"<< rec.count();
//插入记录
query->prepare("insert into table2 values(?,?,?)");
for(int i = 1; i <= 6; i++)
{
query->bindValue(0, name[6-i]);
query->bindValue(1, stuID[i-1]);
query->bindValue(2, address[i-1]);
ok = query->exec();
if(!ok){
QSqlError lasterror= query->lastError();
qDebug()<< lasterror.driverText()<< "table2数据插入失败";
}
}
}
void MainWindow::displaySql()
{
table1Modeldel->setTable("table1");
table1Modeldel->select();
ui->tbView_table1->setModel(table1Modeldel);
table2Modeldel->setTable("table2");
table2Modeldel->select();
ui->tbView_table2->setModel(table2Modeldel);
}
void MainWindow::on_pushButton_clicked()
{
QString sql;
sql = ui->textEdit->toPlainText();
retModel->setQuery(sql);
if(retModel->lastError().isValid())
QMessageBox::critical(NULL, "错误",
retModel->lastError().driverText()+"\n"
+retModel->lastError().databaseText(),
QMessageBox::Yes, QMessageBox::Yes);
ui->tbView_ret->setModel(retModel);
}
王二空间
- 粉丝: 7305
- 资源: 2087
最新资源
- 12Cr1MoV钢主蒸汽管道焊接工艺.pdf
- 12Cr1MoV小径管焊接操作工艺 - .pdf
- 12Cr1MoV异种钢焊接温度场的数值模拟.pdf
- 12Cr2Mo1R珠光体耐热钢的焊接 - .pdf
- 13MnNiMoNbR复合银板焊接裂纹的修复.pdf
- 12MnNiVR钢板焊接裂纹敏感性的试验研究.pdf
- 12MnNiVR钢热处理性能及大热输入焊接韧性.pdf
- 13MnNiMoNbR钢焊接工艺研究.pdf
- 13MnNiMoNbR压力容器高强钢焊接工艺研究.pdf
- 14Cr1MoR新钢种的焊接试验研究.pdf
- 14Cr1MoR-I-0Cr18Ni10Ti复合钢板的焊接 - .pdf
- 13焊接空心球节点的承载能力分析.pdf
- 14CrMoR耐热钢压力容器的埋弧焊焊接工艺应用 - .pdf
- 14CrlMoR+0Crl8Nil0Ti复合钢板焊接.pdf
- 14YTJ108 复合保温钢筋焊接网架混凝土墙(CL建筑体系)构造.pdf
- 15×104_m3双盘浮顶油罐底板焊接工艺.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈