1

I know SO is not a "debug my code service" but after checking and retrying for hours either I'm missing something very stupid or maybe there's a bug (or bad compilation) in my MySQLdb module...

Also I've some related question I've put along with the posted code...

def NextDocumentIdToCache():
 if not OpenConnection():
 return 0
 # ...setting string values... #
 cur = connection.cursor(mysql.cursors.DictCursor)
 cur.execute('SELECT * FROM documents WHERE resolutions <> %s AND pdf > 0 AND status = %s AND expire > %s AND locked = %s LIMIT 0,1', (resolutions, status, expireLimit, ''))
 rowsCount = cur.rowcount
 if rowsCount==0:
 return 0
 row = cur.fetchone()
 id = row['document_id'] 

Everything ok since now. Connection opens, I get one row and retrieve the correct id that is returned at the end of the function.

Then I need to perform an update operation on the row fetched...

 cur.close()
 cur = connection.cursor(mysql.cursors.Cursor) 

I've closed the cursor and opened a new one. Do I actually need to do this? Or can I reuse the same cursor?

 cur.execute("""UPDATE documents SET locked = %s WHERE document_id = %s""", ("worker: rendering pages", id))

This actually doesn't update the row. No exceptions happens, just don't work.

Finally the function ends...

 cur.close()
 return id

Also a couple of questions.

What's the difference between

cur.execute("""UPDATE documents....

and

cur.execute("UPDATE documents....

I've seen around both versions. What's the functional difference between triple double-quote, single double-quote, single single-quote?

And finally

If I write the query

cur.execute("""UPDATE documents SET locked = %s WHERE document_id = %d""", ("worker: rendering pages", id))

(note the %d instead of %s) I get an error. But id is a long integer, I've checked. So what's wrong?

Thank you

asked Sep 28, 2013 at 13:58

1 Answer 1

2

The change doesn't take effect:

Are you doing a transaction? You'll need to commit() if that's the case. PEP 249, which defines the Python Database API, states that a connection will use transactions even if you have not explicitly enabled them: "Note that if the database supports an auto-commit feature, this must be initially off."

Quoting

Triple quoted strings are multiline, single quoted strings aren't.

Interpolation

Because this is not actually string interpolation. Those placeholders are interpreted by MySQLdb, which will handle quoting for you.

Damian Yerrick
4,6942 gold badges28 silver badges69 bronze badges
answered Sep 28, 2013 at 14:05
Sign up to request clarification or add additional context in comments.

1 Comment

Not explicitly (I didn't wrote code to start a transaction. Just opened the connection and executed the two queries) However adding connection.commit() at the end solved the prolem. Thank you.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.