should I reuse the cursor in the python MySQLdb module

后端 未结 1 1079
灰色年华
灰色年华 2020-12-17 09:57

I\'m writing a python CGI script that will query a MySQL database. I\'m using the MySQLdb module. Since the database will be queryed repeatedly, I wrote this function....<

相关标签:
1条回答
  • 2020-12-17 10:22

    The MySQLdb developer recommends building an application specific API that does the DB access stuff for you so that you don't have to worry about the mysql query strings in the application code. It'll make the code a bit more extendable (link).

    As for the cursors my understanding is that the best thing is to create a cursor per operation/transaction. So some check value -> update value -> read value type of transaction could use the same cursor, but for the next one you would create a new one. This is again pointing to the direction of building an internal API for the db access instead of having a generic executeSql method.

    Also remember to close your cursors, and commit changes to the connection after the queries are done.

    Your getDatabaseResult function doesn't need to have a connect for every separate query though. You can share the connection between the queries as long as you act responsible with the cursors.

    0 讨论(0)
提交回复
热议问题