Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 30th, 2012, 08:50 AM
aer0x aer0x is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 aer0x User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.


Reply With Quote
  #2  
Old October 31st, 2012, 07:34 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,759 Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 38 m 3 sec
Reputation Power: 1568
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.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

Last edited by Schol-R-LEA : October 31st, 2012 at 07:44 AM.

Reply With Quote
  #3  
Old October 31st, 2012, 09:33 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,353 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 8 h 3 m 36 sec
Reputation Power: 383
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!

Reply With Quote
  #4  
Old November 1st, 2012, 01:15 AM
aer0x aer0x is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 aer0x User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Validating a MySQLdb Connection in Python?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap