Search code examples
pythonmysqlcleardb

Which python library required for cleardb mysql?


I know that I need to "import MySQLdb" to connect to a MySQL database. But what is the name of the library that we need to import when we are using "cleardb mysql"?

I am hard coding as below to connect, but I guess due to wrong library, I am getting errors. Below are my points to explain my situation::

1) I have installed "MySQldb", and imported it through import keyword.

2) when I use port number in the connectivity syntax, I got "TypeError: an integer is required".

db = MySQLdb.connect("server_IP",3306,"uid","pwd","db_name")

so I removed the port number

import MySQLdb
db = MySQLdb.connect("server_IP","uid","pwd","db_name")
cur = db.cursor()

and the error vanishes. Is that the right method?

3) Everything goes fine until I execute the function "curson.execution("SELECT VERSION()")" to execute sql queries.

curson.execution("SELECT VERSION()")

I am getting error as below

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    cursor.execute("use d_7fc249f763d6fc2")
  File "path_to\cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "path_to\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (2006, 'MySQL server has gone away')

So, is this happening due to library that I have imported? or, if the library is correct, then what seems to be the issue?


Solution

  • The port number is the fifth positional argument, not the second.

    db = MySQLdb.connect("server_IP", "uid", "pwd", "db_name", 3306)