icanc
icanc

Reputation: 3577

Getting a "TypeError: an integer is required" when trying to connect to MySQL

Total Python newb here.

I'm following the tutorial from this page. But when I run my code I get the following error in the console.

Traceback (most recent call last):
  File "/Users/tejenshrestha/Documents/ProjectAccess/access/trunk/analysis/src/json_xml/Database.py", line 37, in <module>
    con = MySQLdb.connect(socket, dbhost, dbuser, dbpass, dbname);
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
TypeError: an integer is required

Searching some solutions I found required that the unix_socket path to be include, which I did as you can see.

I'm also using XAMPP and the socket path I'm using is the one specified in XAMPP's my.cnf file. I have MySQL running and the port it is 3306.

Please help out this noob.

The Python code:

import MySQLdb
import sys

con = None
dbhost = "localhost"
port   = "3306"
dbuser = "root"
dbpass = "password"
dbname = "name"
socket = "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock"

try:
    con = MySQLdb.connect(socket, dbhost, dbuser, dbpass, dbname);
    cur = con.cursor()
    cur.execute("SELECT VERSION()")

    data = cur.fetchone()
    print "Database version : %s " % data

except MySQLdb.Error, e:
    print "Error %d: %s" % (e.args[0],e.args[1])
    sys.exit(1)

finally:    
    if con:    
        con.close()

Upvotes: 0

Views: 3704

Answers (1)

BenDundee
BenDundee

Reputation: 4521

Take the quotes from around port and see if that works.


<brilliant code>
port = 3306
<brilliant code>

I think you need to label the arguments in your connect function, see the top answer here:

How do I connect to a MySQL Database in Python?

Upvotes: 4

Related Questions