Пример #1
2
        void FenPrincipale::submitChangement()
        {
            QSqlTableModel *model = qobject_cast<QSqlTableModel *>(tabless->model());
            model->database().transaction();
               if (model->submitAll()) {
                   model->database().commit();
               } else {
                   model->database().rollback();
                   QMessageBox::warning(this, tr("Cached Table"),
                                        tr("The database reported an error: %1")
                                        .arg(model->lastError().text()));
               }

        }
TableSchema::TableSchema(const QString &table, const QString &env)
    : tablename(table)
{
    if (!dbSettings) {
        QString path = QLatin1String("config") + QDir::separator() + "database.ini";
        if (!QFile::exists(path)) {
            qCritical("not found, %s", qPrintable(path));
        }
        dbSettings = new QSettings(path, QSettings::IniFormat);
    }

    if (openDatabase(env)) {
        if (!tablename.isEmpty()) {
            QSqlTableModel model;
            model.setTable(tablename);
            tableFields = model.record();

            if (model.database().driverName().toUpper() == "QPSQL") {
                // QPSQLResult doesn't call QSqlField::setAutoValue(), fix it
                for (int i = 0; i < tableFields.count(); ++i) {
                    QSqlField f = tableFields.field(i);
                    if (f.defaultValue().toString().startsWith(QLatin1String("nextval"))) {
                        f.setAutoValue(true);
                        tableFields.replace(i, f);
                    }
                }
            }
        } else {
            qCritical("Empty table name");
        }
    }
}
Пример #3
0
void QgsBookmarks::addClicked()
{
    QSqlTableModel *model = qobject_cast<QSqlTableModel *>( lstBookmarks->model() );
    Q_ASSERT( model );

    QgsMapCanvas *canvas = QgisApp::instance()->mapCanvas();
    Q_ASSERT( canvas );

    QSqlQuery query( "INSERT INTO tbl_bookmarks(bookmark_id,name,project_name,xmin,ymin,xmax,ymax,projection_srid)"
                     "  VALUES (NULL,:name,:project_name,:xmin,:xmax,:ymin,:ymax,:projection_srid)",
                     model->database() );

    QString projStr( "" );
    if ( QgsProject::instance() )
    {
        if ( !QgsProject::instance()->title().isEmpty() )
        {
            projStr = QgsProject::instance()->title();
        }
        else if ( !QgsProject::instance()->fileName().isEmpty() )
        {
            QFileInfo fi( QgsProject::instance()->fileName() );
            projStr = fi.exists() ? fi.fileName() : "";
        }
    }

    query.bindValue( ":name", tr( "New bookmark" ) );
    query.bindValue( ":project_name", projStr );
    query.bindValue( ":xmin", canvas->extent().xMinimum() );
    query.bindValue( ":ymin", canvas->extent().yMinimum() );
    query.bindValue( ":xmax", canvas->extent().xMaximum() );
    query.bindValue( ":ymax", canvas->extent().yMaximum() );
    query.bindValue( ":projection_srid", QVariant::fromValue( canvas->mapSettings().destinationCrs().srsid() ) );
    if ( query.exec() )
    {
        model->setSort( 0, Qt::AscendingOrder );
        model->select();
        lstBookmarks->scrollToBottom();
        lstBookmarks->setCurrentIndex( model->index( model->rowCount() - 1, 1 ) );
        lstBookmarks->edit( model->index( model->rowCount() - 1, 1 ) );
    }
    else
    {
        QMessageBox::warning( this, tr( "Error" ), tr( "Unable to create the bookmark.\nDriver:%1\nDatabase:%2" )
                              .arg( query.lastError().driverText() )
                              .arg( query.lastError().databaseText() ) );
    }
}