void QgsBookmarks::on_btnUpdate_clicked()
{
  // get the current item
  QTreeWidgetItem *item = lstBookmarks->currentItem();
  if ( item )
  {
    // make sure the user really wants to update this bookmark
    if ( QMessageBox::Ok == QMessageBox::information( this, tr( "Really Update?" ),
         tr( "Are you sure you want to update the %1 bookmark?" ).arg( item->text( 0 ) ),
         QMessageBox::Ok | QMessageBox::Cancel ) )
    {
      // retrieve the current map extent
      QgsRectangle viewExtent = QgisApp::instance()->mapCanvas()->extent();

      int rc;
      QgsDebugMsg( QString( "Opening user database: %1" ).arg( QgsApplication::qgisUserDbFilePath() ) );
      rc = connectDb();
      if ( SQLITE_OK == rc )
      {
        // prepare the sql statement
        QString sql;
        QTextStream sqlStream( &sql );
        // use '17 g' format; SmartNotation is default
        sqlStream.setRealNumberPrecision( 17 );
        sqlStream << "update tbl_bookmarks set " <<
        "xmin=" << viewExtent.xMinimum() << "," <<
        "ymin=" << viewExtent.yMinimum() << "," <<
        "xmax=" << viewExtent.xMaximum() << "," <<
        "ymax=" << viewExtent.yMaximum() << " " <<
        "where bookmark_id=" << item->text( 3 );
        QgsDebugMsg( QString( "Storing bookmark using: %1" ).arg( sql ) );

        char * errmsg;
        rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg );
        if ( rc != SQLITE_OK )
        {
          // XXX Provide popup message on failure?
          QMessageBox::warning( this, tr( "Error updating bookmark" ),
                                tr( "Failed to update the %1 bookmark. The database said:\n%2" )
                                .arg( item->text( 0 ) ).arg( errmsg ) );
          sqlite3_free( errmsg );
        }
        // close the database
        sqlite3_close( db );

        refreshBookmarks();

      }
      else
      {
        QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( db ) ) );

        // XXX This will likely never happen since on open, sqlite creates the
        //     database if it does not exist.
        assert( rc == 0 );
      }
    }
  }
}
Пример #2
0
void QgsBookmarkItem::store()
{
  // To store the bookmark we have to open the database and insert
  // the record using the parameters set in the constructor

  sqlite3 *db;
  int rc;
  QgsDebugMsg( QString( "Opening user database: %1" ).arg( mUserDbPath ) );
  rc = sqlite3_open( mUserDbPath.toUtf8().data(), &db );
  if ( rc )
  {
    QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( db ) ) );

    // XXX This will likely never happen since on open, sqlite creates the
    //     database if it does not exist.
    assert( rc == 0 );
  }
  // prepare the sql statement
  const char *pzTail;
  sqlite3_stmt *ppStmt;
  QString sql;
  QTextStream sqlStream( &sql );
  sqlStream << "insert into tbl_bookmarks values(null,'" <<
  mName << "','" <<
  mProjectTitle << "'," <<
  mViewExtent.xMinimum() << "," <<
  mViewExtent.yMinimum() << "," <<
  mViewExtent.xMaximum() << "," <<
  mViewExtent.yMaximum() << "," <<
  mSrid << ")";

  QgsDebugMsg( QString( "Storing bookmark using: %1" ).arg( sql ) );

  QByteArray sqlData = sql.toUtf8();

  rc = sqlite3_prepare( db, sqlData.constData(), sqlData.size(), &ppStmt, &pzTail );
  // XXX Need to free memory from the error msg if one is set
  if ( rc == SQLITE_OK )
  {
    // get the first row of the result set
    if ( sqlite3_step( ppStmt ) != SQLITE_DONE )
    {

      // XXX query failed -- warn the user some how
      QgsDebugMsg( QString( "Failed to store bookmark: %1" ).arg( sqlite3_errmsg( db ) ) );
    }
    // close the statement
    sqlite3_finalize( ppStmt );
    // close the database
    sqlite3_close( db );
  }


}
Пример #3
0
std::string SqlUtils::determineSql(std::string sqlFilename) {
	std::string sql;
	std::string sqlFile(resourcePath + "/" + sqlFilename);
	std::ifstream sqlStream(sqlFile.c_str());
	if (sqlStream.good()) {
		std::getline(sqlStream, sql);
		sqlStream.close();
	} else {
		std::string errmsg("File ");
		errmsg.append(sqlFile);
		errmsg.append(" does not exist.\n");
		throw std::runtime_error(errmsg);
	}
	return sql;
}