0

Here's the code I'm working on:

poljeID = int(cursor.execute("SELECT poljeID FROM stanje"))
xkoord = cursor.execute("SELECT xkoord FROM polje WHERE poljeID = %s;", poljeID)
ykoord = cursor.execute("SELECT ykoord FROM polje WHERE poljeID = %s;", poljeID)

print xkoord, ykoord

It's a snippet from it, basically what it needs to do is fetch the ID of the field (poljeID) where an agent is currently on (stanje) and use it to get the x and y coordinates of that field (xkoord, ykoord).

The initial values for the variables are:

poljeID = 1
xkoord = 0
ykoord = 0

The values that I get with that code are:

poljeID = 1
xkoord = 1
ykoord = 1

What am I doing wrong?

1
  • 1
    You should't do database work in your application. Use SELECT xkoord, ykoord FROM polje WHERE poljeID IN (SELECT poljeID FROM stanje). Commented Feb 25, 2014 at 16:02

1 Answer 1

1

cursor.execute does not return the result of the query, it returns the number of rows affected. To get the result, you need to do cursor.fetchone() (or cursor.fetchall()) for each query.

(Note, really the second and third queries should be done at once: SELECT xkoord, ycoord FROM ...)

Sign up to request clarification or add additional context in comments.

1 Comment

Changed it to cursor.execute("SELECT xkoord, ykoord FROM polje WHERE poljeID = %s;", poljeID) xkoord, ykoord = cursor.fetchone() and it works now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.