zoukankan      html  css  js  c++  java
  • Qt数据库2

    一、QSqlQueryModel

          The QSqlQueryModel class provides a read-only data model for SQL result sets.

           QSqlQueryModel is a high-level interface for executing SQL statements and traversing the result set. It is built on top of the lower-level QSqlQuery and can be used to provide data to view classes such as QTableView. For example:

    QSqlQueryModel *model = new QSqlQueryModel;
        model->setQuery("SELECT name, salary FROM employee");
        model->setHeaderData(0, Qt::Horizontal, tr("Name"));
        model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
    
        QTableView *view = new QTableView;
        view->setModel(model);
        view->show();

    二、QSqlTableModel

      The QSqlTableModel class provides an editable data model for a single database table.

      QSqlTableModel is a high-level interface for reading and writing database records from a single table. It is build on top of the lower-level QSqlQuery and can be used to provide data to view classes such as QTableView. For example:

    QSqlTableModel *model = new QSqlTableModel(parentObject, database);
        model->setTable("employee");
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);
        model->select();
        model->setHeaderData(0, Qt::Horizontal, tr("Name"));
        model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
    
        QTableView *view = new QTableView;
        view->setModel(model);
        view->hideColumn(0); // don't show the ID
        view->show();

    We set the SQL table's name and the edit strategy, then we set up the labels displayed in the view header. The edit strategy dictates when the changes done by the user in the view are actually applied to the database. The possible values are OnFieldChange, OnRowChange, and OnManualSubmit.

    QSqlTableModel can also be used to access a database programmatically, without binding it to a view:

    QSqlQueryModel model;
        model.setQuery("SELECT * FROM employee");
        int salary = model.record(4).value("salary").toInt();
  • 相关阅读:
    php_memcache扩展
    window+nginx+php
    服务启动Apache服务,错误Parent: child process exited with status 3 -- Aborting.解决
    PHP文件下载
    gvim
    单车家族 结对项目三
    单车家族 结对项目二
    共享单车 网络定位
    注册页面 android 仿摩拜单车共享单车进度条实现StepView
    结对项目 ——功能结构图
  • 原文地址:https://www.cnblogs.com/wiessharling/p/2856408.html
Copyright © 2011-2022 走看看