Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. python and pyqt question

python and pyqt question

Scheduled Pinned Locked Moved General and Desktop
sql
4 Posts 2 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lukeQt
    wrote on last edited by
    #1

    Hi Everyone,

    I know one cannot have nested sql queries using the same query object. That does not work. The code below works, but I am wondering if this is the best method. In the first query I append the values to a list and then use a for loop to iterate over the list.

    This works, but I am wondering if using a generator here makes more sense? What would you do?

        self.query = QtSql.QSqlQuery()
    
        test = []
    
        # Get id, in_iss_name, rmse, buffer_dist, unit from the database.
        self.query.exec_("select id, iss_name, rmse, buffer_dist, unit, comp_group_lu_id  from in_iss;")
    
        while self.query.next():
            id = self.query.value(0).toInt()[0]
            spatial_data = str(self.query.value(1).toString())
            in_iss_rmse = self.query.value(2).toDouble()[0]
            buffer_dist =  str(self.query.value(3).toString())
            in_iss_unit = str(self.query.value(4).toString())
            comp_group_id = self.query.value(5).toInt()[0]
    
            # Add  the rmse of the issue and the in gtlf together.
            total_rmse = in_iss_rmse  +  self.gtlf_rmse
    
            test.append((id, spatial_data, in_iss_rmse, buffer_dist, in_iss_unit, comp_group_id, total_rmse))
    
    
        for id, spatial_data, in_iss_rmse, buffer_dist, in_iss_unit, comp_group_id, total_rmse in test:
            # This converts the rmse error for all datasets.
            # The rmse value in the database is stored in meters.
            # This is why the source unit is hardcoded.
            total_rmse = self.converter("Meter", self.gtlf_unit, total_rmse)
    
    
        def converter(self, source_unit, dest_unit, amount):
            """
            The converter method takes a source unit name and
            converts the amount to the destination units.
    
            In this case everything gets converted to the map units.
    
            This is done because the map projection units does not have to be
            the same as what the user selects.
           
    
            """
            # Get the con factor from the unit name. This is for the source unit.
            # Query the database and get the con factor
    
            self.query.exec_(QtCore.QString("SELECT con_factor FROM unit_conv_lu WHERE unit = '%1'").arg(source_unit))
            if self.query.next():
                source_con_factor= self.query.value(0).toDouble()[0]
            if self.query.lastError().isValid():
                return self.db_msg()
    
            # Get the con factor from the unit name. This is for the
            # destination unit. Query the database and get the con factor
            self.query.exec_(QtCore.QString("SELECT con_factor FROM unit_conv_lu WHERE unit = '%1'").arg(dest_unit))
    
            if self.query.next():
                destination_con_factor = self.query.value(0).toDouble()[0]
            if self.query.lastError().isValid():
                return self.db_msg()
    
            conversion = amount * source_con_factor / destination_con_factor
            return conversion
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What kind of nested queries do you have in mind ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lukeQt
        wrote on last edited by
        #3

        I query from one table and get the distance and the unit. I then query from the unit table which is where I am storing the con factor. That is a nested query. I then convert the distance into the correct unit. That is what the converter method is doing.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So, do everything here in just one operation ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved