1

I'm using Anaconda and python3.7 and I'm trying to connect to a remote database using the following code:

import MySQLdb
myDB = MySQLdb.connect(host="xxxxx", port=3306, user="xxx",password="xxxx",db="xxxx")

but I get the following error:

  File "C:\Users\zanto\anaconda3\lib\site-packages\MySQLdb\connections.py", line 208, in __init__
    super(Connection, self).__init__(*args, **kwargs2)

OperationalError: (2006, 'SSL connection error: unknown error number')

I tried 2 users in mysql one using % and one using localhost but I still get the same error.

1 Answer 1

1

It was a problem of mySQLdb library.

I installed pymysql library through anaconda and now it's working!

My new code is:

import pymysql.cursors
connection = pymysql.connect(host='xxxx',port=3306,user="xxxx",password="xxxx",db="xxxx", cursorclass=pymysql.cursors.DictCursor)

try:
    with connection.cursor() as cursor:
        sql="SELECT * FROM Table WHERE Field = 'value'"
        cursor.execute(sql)
        results = cursor.fetchall()
        #print (results) #if you remove the comment you will get the query result as a dictionary
        for record in results:
            record_line = " ".join('{0}{1}'.format(field,value) for field,value in record.items())
            print(record_line)

finally:
    connection.close()

More info: A cursor allows Python code to execute MySQL command in a database session. A cursor is created by the connection.cursor() method: they are bound to the connection for the entire lifetime and all the commands are executed in the context of the database session wrapped by the connection.

The cursor.execute() method runs a query in MySQL database. The cursor.fetchall() method returns the results of a query in list form that contains the records as dictionaries.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.