DEV Community

海前 王
海前 王

Posted on

#qt database using sqlite

#qt
#if 0
#include <QApplication>
#include <QMainWindow>
#include <QTableView>
#include <QSqlDatabase>
#include <QSqlTableModel>
#include <QDebug>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <QSqlError> // 需要这个头文
#include <QSqlQuery>  // 确保包含这个头文件

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // 创建主窗口
    QMainWindow window;

    // 设置数据库连接
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("example.db");

    if (!db.open()) {
        qDebug() << "Database error:" << db.lastError().text();
        return -1;
    }

    // 创建表
    QSqlQuery query;
    query.exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)");

    // 插入数据
    query.prepare("INSERT INTO users (name) VALUES (:name)");
    query.bindValue(":name", "John Doe");
    query.exec();

    query.bindValue(":name", "Jane Smith");
    query.exec();

    // 设置模型
    QSqlTableModel *model = new QSqlTableModel;
    model->setTable("users");
    model->select();

    // 创建视图
    QTableView *view = new QTableView;
    view->setModel(model);

    // 创建主窗口的中央部件
    QWidget *centralWidget = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout;
    QPushButton *refreshButton = new QPushButton("Refresh Data");

    layout->addWidget(view);
    layout->addWidget(refreshButton);
    centralWidget->setLayout(layout);

    window.setCentralWidget(centralWidget);

    // 连接按钮点击信号
    QObject::connect(refreshButton, &QPushButton::clicked, [model]() {
        model->select(); // 刷新数据
    });

    query.clear();
    // 显示窗口
    window.resize(600, 400);
    window.show();

    return app.exec();
}
#endif
#include <QApplication>
#include <QMainWindow>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <QSqlDatabase>
#include <QSqlTableModel>
#include <QSqlQuery>
#include <QSqlError>
#include <QDataWidgetMapper>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // 创建主窗口
    QMainWindow window;

    // 设置数据库连接
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("example.db");

    if (!db.open()) {
        qDebug() << "Database error:" << db.lastError().text();
        return -1;
    }

    // 创建表
    QSqlQuery query;
    query.exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, email TEXT)");

    // 插入数据
    query.prepare("INSERT INTO users (name, email) VALUES (:name, :email)");
    query.bindValue(":name", "John Doe");
    query.bindValue(":email", "john.doe@example.com");
    query.exec();

    query.bindValue(":name", "Jane Smith");
    query.bindValue(":email", "jane.smith@example.com");
    query.exec();

    // 设置模型
    QSqlTableModel *model = new QSqlTableModel;
    model->setTable("users");
    model->select();

    // 确保数据加载
    qDebug() << "Column Count:" << model->columnCount();
    for (int i = 0; i < model->rowCount(); ++i) {
        QModelIndex nameIndex = model->index(i, 1);
        QModelIndex emailIndex = model->index(i, 2);
        qDebug() << "Row" << i << "Name:" << model->data(nameIndex).toString()
                 << "Email:" << model->data(emailIndex).toString();
    }

    // 创建控件
     QLineEdit *idEdit = new QLineEdit;
    QLineEdit *nameEdit = new QLineEdit;
    QLineEdit *emailEdit = new QLineEdit;
    QPushButton *nextButton = new QPushButton("Next");
    QPushButton *previousButton = new QPushButton("Previous");

    // 创建映射器
    QDataWidgetMapper *mapper = new QDataWidgetMapper;
    mapper->setModel(model);

    // 映射多个控件
    mapper->addMapping(idEdit,0);

    mapper->addMapping(nameEdit, 1);  // 映射到模型中的第 1 列(name)
    mapper->addMapping(emailEdit, 2); // 映射到模型中的第 2 列(email)

    // 设置映射器的当前索引
    mapper->setCurrentIndex(0);

    // 连接按钮信号
    QObject::connect(nextButton, &QPushButton::clicked, [mapper]() {
        int nextRow = mapper->currentIndex() + 1;
        if (nextRow < mapper->model()->rowCount()) {
            mapper->setCurrentIndex(nextRow);
        }
    });

    QObject::connect(previousButton, &QPushButton::clicked, [mapper]() {
        int previousRow = mapper->currentIndex() - 1;
        if (previousRow >= 0) {
            mapper->setCurrentIndex(previousRow);
        }
    });

    // 创建主窗口的中央部件
    QWidget *centralWidget = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout;
      layout->addWidget(idEdit);
    layout->addWidget(nameEdit);
    layout->addWidget(emailEdit);
    layout->addWidget(nextButton);
    layout->addWidget(previousButton);
    centralWidget->setLayout(layout);

    window.setCentralWidget(centralWidget);

    // 显示窗口
    window.resize(400, 300);
    window.show();

    return app.exec();
}


Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
__040711563a17902392e7 profile image
海前 王
  1. using the sqltablemodel 2.mapping 3.pervious next 4.setcurrent