MySQLdb reconnect

D

Damjan

Does MySQLdb automatically reconnect if the connection to the database is
broken?

I'm asking this since I have a longrunning Python precess that is connected
to Mysql-4.1.11, and I execute "set names utf8" when I connect to it.

But after running a day the results from the python program were displayed
as if the "set names utf8" was not executed i.e. I got question marks where
utf-8 cyrillics should've appeared. After restarting the Python program
everything was ok, just as when I first started it.

The long running Python process is actually a scgi quixote web application.
 
D

Damjan

Does MySQLdb automatically reconnect if the connection to the database is

It seems so.
I'm asking this since I have a longrunning Python precess that is
connected to Mysql-4.1.11, and I execute "set names utf8" when I connect
to it.

But after running a day the results from the python program were displayed
as if the "set names utf8" was not executed i.e. I got question marks
where utf-8 cyrillics should've appeared. After restarting the Python
program everything was ok, just as when I first started it.

This is the sollution I've come to:

try:
# This will fail on MySQL < 4.1
db = MySQLdb.connect(godot.dbhost, godot.dbuser, godot.dbpass,
godot.dbname, use_unicode=1, init_command="set names utf8")
except MySQLdb.OperationalError:
db = MySQLdb.connect(godot.dbhost, godot.dbuser, godot.dbpass,
godot.dbname, use_unicode=1)
db.charset = 'utf8'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top