Frage

I keep getting an error saying my value is not positioned on a valid record. I've executed my query successfully in MySQL, but I think I'm overlooking something in my code.

query = QtSql.QSqlQuery("select patient_id," +
                        "(SUM(IF(uom_id = 1, value, 0)) / SUM(IF(uom_id = 1, 1, 0))) AS `Average radius`," +
                        "(SUM(IF(uom_id = 2, value, 0)) / SUM(IF(uom_id = 2, 1, 0))) AS `Average Volume`," +
                        "(SUM(IF(uom_id = 3, value, 0)) / SUM(IF(uom_id = 3, 1, 0))) AS `Average SA`" +
                        "from measurements" +
                        "WHERE image_id = " + self.dbImage.id.toString() +
                        "AND status = 'A'" +
                        "GROUP BY patient_id", self.db)
query.next()

radius_acc = query.value(1).toDouble()
volume_acc = query.value(2).toDouble()
SA_acc = query.value(3).toDouble()

print('average of previously accepted measurements includes ' +
      'radius = ' + str(radius_acc) +
      'volume = ' + str(volume_acc) +
      'SA = ' + str(SA_acc))

Specifically I want to know if there is something wrong with the way I input the query to QSqlQuery? Or maybe I am trying to use the value(n) method incorrectly?

If neither of those appear to be the case I'll bet I'm using the wrong parameter for self.dbImage.id.toString() in my query, in which case I'll just ask a coworker tomorrow.

War es hilfreich?

Lösung

To iterate over the result set you usually use a while loop:

 while(query.next()){
     // do whatever you need with query.value()
 }

If you're interested only in the first row, you can use first(), again with a while loop:

 while(query.first()){
     // do whatever you need with query.value()
 }

Edit: Sorry, misunderstood your problem. Now I see that you possibly forgot to put some spaces in some strings:

 "from measurements"

to

 " from measurements "
 #^ Here   and here ^^

Without these spaces, your query will look something like

  "...d = 3, 1, 0))) AS `Average SA`from measurementsWHERE image_id = ..."

which of course it's not a valid query.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top