当前位置:网站首页>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 :
边栏推荐
- Printing colored messages on the console with printf
- English learning plan
- Shell notes
- SystemC simulation scheduling mechanism
- Tool notes - common custom tool classes (regular, random, etc.)
- 初学者入门阿里云haas510开板式DTU(2.0版本)--510-AS
- Interview question 17.14 Minimum number of K (almost double hundreds)
- 280 weeks /2171 Take out the least number of magic beans
- My resume.
- 测试工程师如何转型测开
猜你喜欢

Communication flow analysis
![[early knowledge of activities] list of recent activities of livevideostack](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[early knowledge of activities] list of recent activities of livevideostack

chrome://tracing Performance analysis artifact

阿里云开发板vscode开发环境搭建

单总线温度传感器18B20数据上云(阿里云)

Alibaba cloud development board haas510 parses serial port JSON data and sends attributes

Postgresql14 installation and use tutorial

If you want to build brand awareness, what bidding strategy can you choose?
![[advanced MySQL] evolution of MySQL index data structure (IV)](/img/eb/e32387b172eb4c3a4152dbc3b0cb8f.png)
[advanced MySQL] evolution of MySQL index data structure (IV)

PostgreSQL14安装使用教程
随机推荐
Remote code injection
Getting started alicloud haas510 open board DTU (version 2.0) --510-as
969. pancake sorting
Why do Chinese programmers change jobs?
[advanced MySQL] query optimization principle and scheme (6)
Bridging and net
For cross-border e-commerce, the bidding strategy focusing more on revenue - Google SEM
Leetcode 2185. 统计包含给定前缀的字符串
atomic and exclusive operation
使用make方法创建slice切片的坑
Lua common built-in functions
Reverse the encryption parameters of a hot water software
Tree reconstruction (pre order + middle order or post order + middle order)
SystemC:SC_ Thread and SC_ METHOD
Analysis of lua source code
[MySQL] basic database operation
Player practice 15 xdemux and avcodecparameters
Crack WinRAR to ad pop-up window
280 weeks /2171 Take out the least number of magic beans
Copy word content to excel and automatically divide it into multiple columns