pro文件:QT += core gui sql
widget.ui
main.cpp
#include "widget.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w("./student.db"); //传入文件名w.show();return a.exec();
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QSqlDatabase> //数据库连接
#include <QSqlQuery> //执行SQL语句namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QString filename, QWidget *parent = 0); //数据库文件名,存储数据库的操作~Widget();void showInfo();void clearInput();private slots:void on_pb_add_clicked();void on_pb_delete_clicked();void on_pb_query_clicked();void on_pb_update_clicked();private:Ui::Widget *ui;QSqlDatabase db_student; //表示数据库的连接QString db_file_name; //数据库文件名};#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QFile> //文件类
#include <QDebug>
#include <QSqlError>Widget::Widget(QString filename, QWidget *parent) :QWidget(parent),ui(new Ui::Widget),db_file_name(filename)
{ui->setupUi(this);ui->cbb_gender->setCurrentIndex(-1);ui->cbb_gender->setFixedSize(224,40);ui->te_table_content->setReadOnly(true);QFile file(db_file_name); //要操作的文件名if(!file.exists()){//数据库文件不存在表示这个程序第一次被运行//连接数据库,创建数据库表,生成数据库文件//"QSQLITE"表示创建的是基于sqlite数据库的连接db_student = QSqlDatabase::addDatabase("QSQLITE");//设置数据库文件的名字db_student.setDatabaseName(db_file_name);//打开数据库if(!db_student.open()){qDebug() << "打开数据库失败1";}else{qDebug() << "打开数据库成功1";}//QSqlQuery用于SQL语句的执行,对返回结果的使用//要指定使用的数据库的连接 db_studentQSqlQuery sql_query(db_student);//创建数据库表QString str_query = "create table Student(id varchar(64) PRIMARY KEY,name varchar(64),gender varchar(64),age int)";//执行SQL语句if(!sql_query.exec(str_query)){qDebug() << str_query <<"failed";qDebug() << sql_query.lastError().text();}//向数据库表Student插入数据(方式一)str_query = "insert into Student values('2310060001','张三','男',15)";if(!sql_query.exec(str_query)){qDebug() << str_query << "failed";qDebug() << sql_query.lastError().text();}//向数据库表Student插入数据(方式二)str_query = "insert into Student values(?,?,?,?)";//准备SQL语句sql_query.prepare(str_query);//绑定待定的数据,取代SQL语句里的?sql_query.addBindValue("2310060002");sql_query.addBindValue("李四");sql_query.addBindValue("男");sql_query.addBindValue(16);if(!sql_query.exec()){qDebug() << str_query << "failed";qDebug() << sql_query.lastError().text();}//向数据库表Student插入数据(方式三)str_query = "insert into Student values(:id,:name,:gender,:age)";sql_query.prepare(str_query);//绑定待定的数据sql_query.bindValue(":id","2310060003");sql_query.bindValue(":name","王五");sql_query.bindValue(":gender","女");sql_query.bindValue(":age",20);if(!sql_query.exec()){qDebug() << str_query << "failed";qDebug() << sql_query.lastError().text(); //打印具体错误}}else{db_student = QSqlDatabase::addDatabase("QSQLITE");//设置数据库文件的名字db_student.setDatabaseName(db_file_name);//打开数据库if(!db_student.open()){qDebug() << "打开数据库失败2";}else{qDebug() << "打开数据库成功2";}}// 显示Student表中所有的记录showInfo();
}Widget::~Widget()
{delete ui;
}//更新信息显示
void Widget::showInfo()
{QSqlQuery sql_query(db_student); //执行QSL语句QString str_query = "select * from Student";if(!sql_query.exec(str_query)){qDebug() << str_query << "failed";qDebug() << sql_query.lastError().text();}ui->te_table_content->clear();while(sql_query.next()) //获得当前的一整条记录{QString s1;for(int i = 0;i < 4;i++) //id,name,gender,age{s1 += sql_query.value(i).toString(); //获得一条记录中的第i个表项,0 bases1 += " ";}ui->te_table_content->append(s1);}
}//清空
void Widget::clearInput()
{ui->le_id->clear();ui->le_name->clear();ui->cbb_gender->setCurrentIndex(-1);ui->le_age->clear();
}//新增
void Widget::on_pb_add_clicked()
{QSqlQuery sql_query(db_student);QString str_query = "insert into Student values(?,?,?,?)"; //id,name,gender,age//准备SQL语句sql_query.prepare(str_query);//绑定待定的数据,取代SQL语句里的?sql_query.addBindValue(ui->le_id->text());sql_query.addBindValue(ui->le_name->text());sql_query.addBindValue(ui->cbb_gender->currentText());sql_query.addBindValue(ui->le_age->text().toInt());if(!sql_query.exec()){qDebug() << str_query << "failed";qDebug() << sql_query.lastError().text();}clearInput();showInfo();
}//删除
void Widget::on_pb_delete_clicked()
{QSqlQuery sql_query(db_student);QString str_query = "delete from Student where id = :id";sql_query.prepare(str_query);sql_query.bindValue(":id",ui->le_id->text());if(!sql_query.exec()){qDebug() << str_query << "failed";qDebug() << sql_query.lastError().text();}clearInput();showInfo();
}//查询
void Widget::on_pb_query_clicked()
{QSqlQuery sql_query(db_student); //操作的数据库QString str_query = "select * from Student where id = :id";sql_query.prepare(str_query);sql_query.bindValue(":id",ui->le_id->text());if(!sql_query.exec()){qDebug() << str_query << "failed";qDebug() << sql_query.lastError().text();}while(sql_query.next()) //获得当前的一整条记录 id,name,gender,age{ui->le_name->setText(sql_query.value(1).toString());ui->cbb_gender->setCurrentText(sql_query.value(2).toString());ui->le_age->setText(sql_query.value(3).toString());}
}//修改
void Widget::on_pb_update_clicked()
{QSqlQuery sql_query(db_student);QString str_query = "update Student set name = ?,gender = ?,age = ? where id = ?";sql_query.prepare(str_query);sql_query.addBindValue(ui->le_name->text());sql_query.addBindValue(ui->cbb_gender->currentText());sql_query.addBindValue(ui->le_age->text().toInt());sql_query.addBindValue(ui->le_id->text());if(!sql_query.exec()){qDebug() << str_query << "failed";qDebug() << sql_query.lastError().text();}clearInput();showInfo();
}
结果
新增
删除
查询
修改