473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

MySQLdb reconnect

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.
--
damjan
Jul 19 '05 #1
2 5779
I don't beleive that it does. You can however call ping() on the
connection which should attempt an automatic reconnection.

See the docs for mysql_ping:

http://dev.mysql.com/doc/mysql/en/mysql-ping.html

I've never tested that, but I have a need for it also so let me know if
it works or not.

-Dan

Jul 19 '05 #2
> Does MySQLdb automatically reconnect if the connection to the database is
broken?
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'
--
damjan
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave Harrison | last post by:
Hi all, got a problem combinging mx and MySQLdb, when I build and install both for my Python2.1 install on a Solaris 9 box I can import mx fine, but importing MySQLdb causing python to core dump. ...
1
by: Peter Nikolaidis | last post by:
Greetings, I am attempting to get MySQLdb 0.9.2 installed on Mac OS 10.2 with a Fink distribution of Python 2.2.2. I have seen only a few posts on the subject, some of them relate to...
1
by: Derek Fountain | last post by:
I was trying to use MySQLdb to connect to a database. All is OK, except I can't figure out how to get the details of an error. Suppose I try to connect to a non existant server, or with the wrong...
2
by: Tim Williams | last post by:
I'm trying to write a simple python program to access a MySQL database. I'm having a problem with using MySQLdb to get the results of a SQL command in a cursor. Sometimes the cursor.execute works,...
2
by: ws Wang | last post by:
MySQLdb is working fine at command line, however when I tried to use it with mod_python, it give me a "server not initialized" error. This is working fine: ----------------------- testmy.py...
1
by: Steve | last post by:
Darwin steve.local 8.3.0 Darwin Kernel Version 8.3.0: Mon Oct 3 20:04:04 PDT 2005; root:xnu-792.6.22.obj~2/RELEASE_PPC Power Macintosh powerpc MacOSX 10.4.3 mysql Ver 14.7 Distrib 4.1.14, for...
7
by: Bernard Lebel | last post by:
Hello, I'm stumbled at a serious problem, and quite frankly getting desparate. This is a rather long-winded one so I'll try to get straight to the point. I have this Python program, that...
1
by: Yi Xing | last post by:
Hi, I met the following error when I tried to install MySQLdb. I had no problem installing numarray, Numeric, Rpy, etc. Does anyone know what's the problem? Thanks! running install running...
0
by: Steve Holden | last post by:
Vaibhav.bhawsar wrote: imported The point here is that MySQLdb is a package, not a module. Some packages have their top-level __init__.py import the package's sub-modules or sub-packages to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.