#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep 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.

  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    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.
    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 Short Understanding the C/C++ Preprocessor
    Taming Python A 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
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    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 and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep 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!!

IMN logo majestic logo threadwatch logo seochat tools logo