这篇帖子是我最近学习QT的sqlite数据库整理的,之前学的是MySQL应用与Java,现在重新捡起来,小白可以看看,特别简单适合小白。
一、首先看一下布局,形成一个整体的思路
1、添加书获取左边lineEdit的内容
2、删除时通过学号删除。
3、修改是通过学号或者姓名修改
4、查找时上面的文本框输入sql语句(select * from StuInfo where age=22;),然后点击查询,下面的文本框显示结果。
二、数据库内容:
1、字段包括:学号(num)、姓名(name)、年龄(age)、成绩(score)。
看图:
三、工程目录的文件夹包括
如图:
1、数据库的连接、打开、初始化建表放在主函数里面(ps:不要将数据库的关闭放在主函数里面)
上代码:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//创建数据库
//创佳数据库连接句柄
QSqlDatabase mydb=QSqlDatabase::addDatabase("QSQLITE");
//关联数据库句柄与对应的数据库文件
mydb.setDatabaseName("stu.db");
//打开数据库
if(!mydb.open())
{
qDebug()<<"数据库打开失败"<<mydb.lastError().text();
}
//执行sql命令
QSqlQuery query;
QString sql="create table if not exists StuInfo(num integer PRIMARY KEY, \
name text, age integer, score real);";
if(!query.exec(sql))
{
qDebug()<<"sql语句执行失败:"<<query.lastError().text();
}
//关闭 不能在这里关闭
MainWindow w;
w.show();
return a.exec();
}
2、然后就是就是对于数据的操作,增加、删除、查找、修改。
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
//添加数据
QSqlQuery query;
QString sql=QString("insert into StuInfo(num,name,age,score) values (%1,'%2',%3,%4)")
.arg(ui->lineEdit_num->text())
.arg(ui->lineEdit_name->text())
.arg(ui->lineEdit_age->text())
.arg(ui->lineEdit_score->text());
if(query.exec(sql))
{
qDebug()<<"添加成功";
}
else
{
qDebug()<<"添加失败:"<<query.lastError().text();
}
}
void MainWindow::on_pushButton_2_clicked()
{
//通过学号删除
QSqlQuery query;
QString sql=QString("delete from StuInfo where num=%1").arg(ui->lineEdit_num->text());
if(query.exec(sql))
{
qDebug()<<"删除成功";
}
else
{
qDebug()<<"删除失败:"<<query.lastError().text();
}
}
void MainWindow::on_pushButton_3_clicked()
{
//修改
QSqlQuery query;
QString sql=QString("update StuInfo set age=%1 ,score=%2 where num=%3 or name='%4';")
.arg(ui->lineEdit_age->text())
.arg(ui->lineEdit_score->text())
.arg(ui->lineEdit_num->text())
.arg(ui->lineEdit_name->text());
if(query.exec(sql))
{
qDebug()<<"修改成功";
}
else
{
qDebug()<<"修改失败:"<<query.lastError().text();
}
}
void MainWindow::on_pushButton_4_clicked()
{
//查询
QSqlQuery query;
QString sql=QString(ui->textEdit->toPlainText());
if(query.exec(sql))
{
qDebug()<<"查询成功";
}
else
{
qDebug()<<"查询失败:"<<query.lastError().text();
}
//定义数据记录的对象,拿到select命令的执行结果
QSqlRecord record=query.record();
//返回数据字段的个数
int n=record.count();
//显示数据select * from StuInfo where age=22;
while(query.next())
{
int num=query.value(0).toInt();
QString name=query.value(1).toString();
int age=query.value(2).toInt();
double score=query.value(3).toDouble();
qDebug()<<num<<name<<age<<score;
QString all=query.value(0).toString()+"\t"+name+"\t"+query.value(2).toString()+"\t"+query.value(3).toString();
qDebug()<<all;
ui->textEdit_ret->setText(all);
}
}
注意:可能这里里面的控件的命名跟你的不一样,复制粘贴代码的时候应该注意下(大佬就不用看了)。
此外:关于sqlite数据库的内容的话,目前我觉得需要注意的是,sql语句正确性是关键,然后没有啥了。如果需要源码的可以私聊。
像扣篮的程序员小李一直在努力,有用的老铁记得点赞。拜拜。