当前位置:网站首页>QT to realize the simple use of SQLite database
QT to realize the simple use of SQLite database
2022-06-12 14:19:00 【silence_ heart】
qt Realization sql operation , First add in the project file
QT+=sql
The required header file
#include<QDebug>
#include<QDir>
#include<QtSql/QSqlDatabase>
#include<QtSql/QSqlError>
#include<QtSql/QSqlTableModel>
#include<QtSql/QSqlQuery>
stay mainwindow.h Define variables in , For database operations
QSqlDatabase db;
QSqlTableModel *model;
int currentSelectRow;
Define the database initialization function
bool MainWindow::initDB(){
// load sqlite drive
if(QSqlDatabase::contains("qt_sql_default_connection")){
db=QSqlDatabase::database("qt_sql_default_connection");
}else{
db=QSqlDatabase::addDatabase("QSQLITE");
}
// Determine whether the database file exists
bool dbFile = QFile::exists(QDir::currentPath()+"/mydb.db");
// Open database , If the database file does not exist, it will be created automatically
db.setDatabaseName(QDir::currentPath()+"/mydb.db");
if(!db.open()){
qDebug()<<"database open error!"<<db.lastError();
return false;
}
QSqlQuery query(db);
// If you have not created a database file , Create a new table
if(!dbFile){
query.exec("CREATE TABLE table_1 ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"name text, "
"age integer)"
);
}
model=new QSqlTableModel();
model->setTable("table_1");
model->select();
ui->tableView->setModel(model);
// Settings table is not editable
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}
stay main Reference... In function
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.initDB();
return a.exec();
}
stay ui Design graphical interface in editor 
Select the row where the current cell is located , And display the selected data
void MainWindow::on_tableView_clicked(const QModelIndex &index)
{
currentSelectRow=ui->tableView->currentIndex().row();
ui->tableView->selectRow(currentSelectRow);
// Display the selected data
ui->lineEdit->setText(model->data(model->index(currentSelectRow,0)).toString());
ui->lineEdit_2->setText(model->data(model->index(currentSelectRow,1)).toString());
ui->lineEdit_3->setText(model->data(model->index(currentSelectRow,2)).toString());
}
Define the callback function for adding buttons , Implement data addition
void MainWindow::on_pushButton_2_clicked()
{
int rowNum=model->rowCount();
model->insertRow(rowNum);
model->setData(model->index(rowNum,1),ui->lineEdit_2->text());
model->setData(model->index(rowNum,2),ui->lineEdit_3->text());
model->submitAll();
model->select();
}
Define the callback function for the delete button , Realize data deletion
void MainWindow::on_pushButton_3_clicked()
{
model->removeRow(currentSelectRow);
model->submitAll();
model->select();
}
Define the callback function of the Modify button , Realize data modification
void MainWindow::on_pushButton_4_clicked()
{
model->setData(model->index(currentSelectRow,1),ui->lineEdit_2->text());
model->setData(model->index(currentSelectRow,2),ui->lineEdit_3->text());
model->submitAll();
model->select();
}
Define the callback function of the query button , Realize to name Fuzzy query of
void MainWindow::on_pushButton_clicked()
{
if(ui->lineEdit_4->text().isEmpty())
{
model->setFilter("");
}else{
QString str(ui->lineEdit_4->text());
model->setFilter("name like '%"+str+"%'");
}
model->select();
}
effect :
边栏推荐
- [advanced MySQL] evolution of MySQL index data structure (IV)
- 阿裏雲開發板HaaS510報送設備屬性
- 拆改廣告機---業餘解壓
- Tlm/systemc: TLM socket binding problem
- Alibaba cloud development board haas510 responds to UART serial port instructions
- Shell脚本到底是什么高大上的技术吗?
- PostgreSQL14安装使用教程
- Running phase of SystemC
- If you want to build brand awareness, what bidding strategy can you choose?
- QA of some high frequency problems in oauth2 learning
猜你喜欢

Leetcode 2185. Counts the string containing the given prefix

Llvm pass-- virtual function protection

Implementation and debug of process hiding under x64

Player practice 20 audio thread and video thread

Player actual combat 25 unpacking module add close

En langage C, la fonction principale appelle une autre fonction et assemble le Code pour comprendre

Summary of virtual box usage problems

Introduction to database system (Fifth Edition) notes Chapter 1 Introduction

Shell脚本到底是什么高大上的技术吗?

Crack WinRAR to ad pop-up window
随机推荐
After reading the question, you will point to offer 16 Integer power of numeric value
NotePad 常用设置
Compile and install lamp architecture of WordPress and discuz for multi virtual hosts based on fastcgi mode
How to set, reset and reverse bit
Analysis of lua source code
动态搜索广告智能查找匹配关键字
Leetcode 2176. 统计数组中相等且可以被整除的数对
Conversion of player's actual 10 pixel format and size
什么是自动出价?它的优势是什么?
SystemC common errors
Word insert picture blocked by text
En langage C, la fonction principale appelle une autre fonction et assemble le Code pour comprendre
Alibaba Cloud Development Board haas510 submission Device Properties
Llvm pass-- virtual function protection
How to brush leetcode
Bridging and net
Details of bypassing safeseh mechanism by using modules that do not enable safeseh
Program analysis and Optimization - 6 loop optimization
Démontage et modification de la machine publicitaire - décompression amateur
Mold and remainder