python and pyqt question
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() spatial_data = str(self.query.value(1).toString()) in_iss_rmse = self.query.value(2).toDouble() 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() # 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() 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() if self.query.lastError().isValid(): return self.db_msg() conversion = amount * source_con_factor / destination_con_factor return conversion
What kind of nested queries do you have in mind ?
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.
So, do everything here in just one operation ?