[PD] Developing a python-mysqql-database-soundfile-player was: accessing mysql database

sven ml.sven at subscience.de
Sat Apr 14 13:57:50 CEST 2007


What still does not work, but its not a problem since i "hardcoded"  
the connection setup to the server.
But sending the message

connect localhost root user pwd database(

gives me

could not log into MySQL Server.

defining the variables within the script does work.

mysql_host = "localhost"
mysql_user = "root"
mysql_pwd = "user"
mysql_db = "pwd"

The python-code is (Thanks to Georg Holzmann):
------------------------------------------------------------------------ ---
# methods

def connect(host_="", user_="", db_name_="", pwd_=""):
        """ connect to MySQL server """
        global db
        global cursor
        try:
                db = MySQLdb.connect(host=str(host_), user=str(user_), passwd=str (pwd_), db=str(db_name_))
                cursor = db.cursor()
                print "PD mysql: logged into MySQL server !"
                return 1
        except:
                print "PD mysql: could not log into MySQL server !"
                return 0
------------------------------------------------------------------------ ------------------------------------------------------------------------

there's lot's of different things that can go wrong when connecting to a db.
the very generic try,except clause here is problematic because it doesn't really
help you finding the exact error.
use the code below to know what exactly went wrong and let us know the error message.
add the "print traceback.format_exc()" also to your execute function.
concerning getting the results, indexing lists on so on the best idea is to store
the results inside python and access it via python functions so indexing is easy, clean and
reliable and you don't have to bother trying that directly in pd (which is a
major PITA if not impossible).
and while this is likely to become a bit more complex it's also a good idea to wrap up
the whole mess into a class anyway.
i can give you an example later today if you don't know what i mean.

- - -
import traceback

def connect(host_="", user_="", db_name_="", pwd_=""):
    """ connect to MySQL server """
    global db
    global cursor
    try:
        db = MySQLdb.connect(host=str(host_), user=str(user_), passwd=str (pwd_), db=str(db_name_))
        cursor = db.cursor()
        print "PD mysql: logged into MySQL server !"
        return 1
    except:
        print '-'*76
        print "MySQL Error!"
        print '-'*76
        print traceback.format_exc()
        print '-'*76
        return 0
- - -





More information about the Pd-list mailing list