473,396 Members | 2,018 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.

Getting error condition from MySQLdb

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 password - how do I get
a meaningful error message which I can present to my user?

Jul 18 '05 #1
1 2978
Derek Fountain wrote:
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 password - how do I get
a meaningful error message which I can present to my user?


In Python whenever an error occurs an exception is raised. I'd suggest a
quick read of the section of the tutorial that explains this;

http://www.python.org/doc/current/tut/node10.html

Then fire up a Python command prompt and try some things out. For
instance, on my Windows machine if I try;
import MySQLdb
mydb=MySQLdb.connect(db='wibble')


I get a nice traceback;

Traceback (most recent call last):
File "<input>", line 1, in ?
File "C:\PROGRA~2\PYTHON23\Lib\site-packages\MySQLdb\__init__.py",
line 63, in Connect
return apply(Connection, args, kwargs)
File "C:\PROGRA~2\PYTHON23\Lib\site-packages\MySQLdb\connections.py",
line 115, in __init__
self._make_connection(args, kwargs2)
File "C:\PROGRA~2\PYTHON23\Lib\site-packages\MySQLdb\connections.py",
line 41, in _make_connection
apply(super(ConnectionBase, self).__init__, args, kwargs)
OperationalError: (2003, "Can't connect to MySQL server on 'localhost'
(10061)")

If I actually start up MySQL, I get;

Traceback (most recent call last):
File "<input>", line 1, in ?
File "C:\PROGRA~2\PYTHON23\Lib\site-packages\MySQLdb\__init__.py",
line 63, in Connect
return apply(Connection, args, kwargs)
File "C:\PROGRA~2\PYTHON23\Lib\site-packages\MySQLdb\connections.py",
line 115, in __init__
self._make_connection(args, kwargs2)
File "C:\PROGRA~2\PYTHON23\Lib\site-packages\MySQLdb\connections.py",
line 41, in _make_connection
apply(super(ConnectionBase, self).__init__, args, kwargs)
OperationalError: (1049, "Unknown database 'wibble'")

Note that MySQLdb, like the majority of distributed modules, defines its
own exceptions. MySQLdb, to the best of my knowledge, implements the
standard DB-API exceptions. They are detailed here;

http://www.python.org/topics/databas...seAPI-2.0.html

Regards,
Andy
--
--------------------------------------------------------------------------------
From the desk of Andrew J Todd esq - http://www.halfcooked.com/

Jul 18 '05 #2

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

Similar topics

3
by: James | last post by:
Please help - getting very desperate! Sun, 12 October 2003 05:39 I have PHPDEV 4.2.3 from Firepages.com.au as the upgrade to 4.3.0 did not work. I also had an abortive download from PHP.NET as...
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...
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,...
3
by: Paul M | last post by:
I encountered the following error when trying to perform a SQL UPDATE to a MySQL database table from Python. I would apprciate any assistance. In the Python code I have tried integer and decimal...
7
by: Robert Ferber | last post by:
Hi, I can't find any good documentation about the Python-database module, all I found was this rudimentary (no examples, no references) piece here: http://www.python.org/peps/pep-0249.html ...
1
by: Yi Xing | last post by:
I log into the machine remotely. How do I check the Mac OSX version number under command line? Thanks. hiaips rosedb0 at gmail.com Wed Aug 16 01:23:10 CEST 2006 * Previous message: MySQLdb...
0
by: Michael Boldin via alt email | last post by:
I installed python 2.5 and used the win package for installing MySQLdb. (Iam running Windows XP) Everything works as expected using python directly (Windows command shell) but using IDLE gives the...
3
by: int32bit | last post by:
I can't figure out why this doesn't work. Any ideas appreciated. conn = MySQLdb.connect (db = "vocab") cursor = conn.cursor () cursor.execute ("SELECT VERSION()") row = cursor.fetchone ()...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.