The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Validating a MySQLdb Connection in Python?
Discuss Validating a MySQLdb Connection in Python? in the Python Programming forum on Dev Shed. Validating a MySQLdb Connection in Python? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 30th, 2012, 08:50 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 2
Time spent in forums: 10 m
Reputation Power: 0
|
|
|
Validating a MySQLdb Connection in Python?
the following code has flaws.
Code:
s = raw_input('Server: ')
u = raw_input('Username: ')
p = raw_input('Passwordr: ')
db = raw_input('Database: ')
try: con = MySQLdb.connect(s,u,p,fb)
if con: print 'successfully connected'
except: print 'unsuccessful connection'
this type of script shows that it is connected without any details being entered.
what is the best way to validate each and every raw_input to allow a connection with the right details and disallow a connection with the wrong or 'no' details?
thanks guys, im slowly learning, google search hasnt offered much either.

|

October 31st, 2012, 07:34 AM
|
 |
Commie Mutant Traitor
|
|
Join Date: Jun 2004
Location: Norcross, GA (again)
|
|
I've tried to look up a way to do what you're asking, but to no avail. I think that the best thing you can do is a) check the user input for empty strings, and b) have the exception frame print out where it fails when it does.
Code:
while '' in [s, u, p, db]:
s = raw_input('Server: ')
u = raw_input('Username: ')
p = raw_input('Password: ')
db = raw_input('Database: ')
try:
con = MySQLdb.connect(s,u,p,fb)
if con:
print 'successfully connected'
except MySQLdb.InterfaceError, e:
print 'unsuccessful connection - "%s"' % e
BTW, there now is an official Python connector that is part of MySQL; you might want to look into using that instead of MySQLdb.
Last edited by Schol-R-LEA : October 31st, 2012 at 07:44 AM.
|

October 31st, 2012, 09:33 AM
|
 |
Contributing User
|
|
|
|
It's best to put only the code you want trapped in the try block. Otherwise you (anyway it often happened to me) won't know what caused the error.
Code:
while '' in [s, u, p, db]:
s = raw_input('Server: ')
u = raw_input('Username: ')
p = raw_input('Password: ')
db = raw_input('Database: ')
try:
con = MySQLdb.connect(s,u,p,fb)
except MySQLdb.InterfaceError, e:
print 'unsuccessful connection - "%s"' % e
# raise # you might want to re-raise the exception.
else: # the connection succeeded.
print 'successfully connected'
Thank you, Scholar Lea, for the indentations.
__________________
[code] Code tags[/code] are essential for python code!
|

November 1st, 2012, 01:15 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 2
Time spent in forums: 10 m
Reputation Power: 0
|
|
|
mmmm no good
thanks for the input guys but the code you both provided also has flaws, and the mysql connector is INcompatible simply because i am running python on mac bootcamp and the specs are incompatible, sucks... but thanks!!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|