3

I'm trying to figure out how to use python's mysqldb. I can do my job with my current knownledge, but I want to use the best practices.

Should I close properly my cursor? Exiting the program isn't close it autmatically? (Shouldn't I expect the object destructor to do it anyway?)

Should I create new cursors for every query, or one cursor is enough for multiple different queries in the same DB?

2
  • Is there any particular reason you want to use MySQLdb instead of something more user friendly like sqlalchemy ? Commented Jun 23, 2011 at 11:25
  • 1
    Or a better wrapper like oursql Commented Jun 23, 2011 at 11:49

1 Answer 1

2

Should I close properly my cursor?

Yes, you should. Explicit is better than implicit.

Should I create new cursors for every query, or one cursor is enough for multiple different queries in the same DB?

This depends on how you use this cursor. For simple tasks it is enough to use one cursor. For some complex application it is better to create separate cursor for each batch of SQL-queries.

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

4 Comments

Thanks for your answer. Well, actually I'm writing a script, but even if I have to create a gui app, it seems more reasonable to create one cursor at the beginning and use that if some query is needed (for example check for new data). I'm not sure if this is correct. Well, my main problem is I haven't find a good tutorial with covering more than the basics or a good documentation. The official (?) documentation seems to assume that I already know everything about the c library
@user747704 I would suggest to use one cursor for the set of queries, that perform one operations (e.g. some check query, update/insert query, one more check/read query). It is a bad idea to use one cursor over a big application.
Can you expand on the reason it's bad to use one cursor app-wide? I'm a little light, knowledge-wise, in that area.
@Michael Different parts of your system may use different cursor types. Also they may want to set some custom properties to cursor object (e.g. 'SET ATHOCOMMIT=0') while others parts need original connection properties.

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.