Friday, October 14, 2016

Using MySQL in a Python Program

Now that I've got MySQL install, and python-MySQL installed in python, it's time to use my database from a python script.

I found a good tutorial here.

When I tried this code, I got an error:

    # Open database connection
    db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )

    # prepare a cursor object using cursor() method
    cursor = db.cursor()

    # execute SQL query using execute() method.
    cursor.execute("SELECT VERSION()")

    # Fetch a single row using fetchone() method.
    data = cursor.fetchone()

    print "Database version : %s " % data

    # disconnect from server
    db.close()  


  OperationalError: (2002, 'Can\'t connect to local MySQL server through socket \'/var/lib/mysql/mysql.sock\' (2 "No such file or directory")')


No comments:

Post a Comment