1 Star 0 Fork 1

sumi/qt_sqlite

forked from aixx3000/qt_sqlite 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mainwindow.cpp 2.42 KB
一键复制 编辑 原始数据 按行查看 历史
aixx3000 提交于 2022-02-12 15:31 . 测试OK1
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "editquerymodel.h"
#include <QtDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_db = QSqlDatabase::addDatabase("QSQLITE");
m_db.setDatabaseName("test.db");
if(m_db.open()){
qDebug() << "open db sucessfull";
QString sqlcmd = "insert into process_monitor_info(time,pro_name,pro_cpu,pro_mem)values('2022-2-11 16:03:37','googlechrome.exe','1','50000')";
QSqlQuery query;
if(query.exec(sqlcmd)){
qDebug() << "insert done";
//查询
QString sqlSelect = "select * from process_monitor_info";
if(query.exec(sqlSelect)){
while (query.next()) {
qDebug() << query.value("pro_name").toString() << query.value("pro_cpu").toInt();
}
}
//删除
QString sqlDel = "delete from process_monitor_info where id = (SELECT MAX(id) from process_monitor_info)";
if(query.exec(sqlDel)){
qDebug() << "delete done";
}
}else{
qDebug() << "insert error" << m_db.lastError();
}
/*
//QSqlQueryModel模型默认只读
QSqlQueryModel* model = new QSqlQueryModel;
model->setQuery("select * from process_monitor_info");
model->setHeaderData(0,Qt::Horizontal,tr("ID"));
model->setHeaderData(1,Qt::Horizontal,tr("进程名"));
model->setHeaderData(2,Qt::Horizontal,tr("CPU"));
model->setHeaderData(3,Qt::Horizontal,tr("MEM"));
*/
//重写QSqlQueryModel UI控件更新到数据库
editQueryModel* model = new editQueryModel;
model->setQuery("select * from process_monitor_info");
model->setHeaderData(0,Qt::Horizontal,tr("ID"));
model->setHeaderData(1,Qt::Horizontal,tr("时间"));
model->setHeaderData(2,Qt::Horizontal,tr("进程"));
model->setHeaderData(3,Qt::Horizontal,tr("CPU"));
model->setHeaderData(4,Qt::Horizontal,tr("MEM"));
QTableView* view = new QTableView(this);
view->setFixedSize(QSize(this->width(),this->height()));
view->setModel(model);
view->show();
//关闭数据库
m_db.close();
}else{
qDebug() << "open faild" << m_db.lastError();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/JaneChow/qt_sqlite.git
[email protected]:JaneChow/qt_sqlite.git
JaneChow
qt_sqlite
qt_sqlite
master

搜索帮助