skip to Main Content

I am taking my first steps in Python, and struggling to make a connection with a small MySql-database which I have created locally.

My local MySql-database works, and its credentials look like this:enter image description here

It works. I can do anything in a database I would like to.
Now comes the hard part.

When I work with a mysql-connector I use this connection, after which the python-script aborts, Python is closed, and no error messages pop up (i.e. no information):

cnx = mysql.connector.connect(user=’root’, password=’Aragorn_2024′, host=’127.0.0.1′, database=’Aragorn’)

Another trace I have tried is using ODBC, with this connection string. This only gives vague notifications, which I have tried to solve, but without the wanted result.

cnx = pyodbc.connect(‘DRIVER={MySQL ODBC 9.1 Ansi Driver};User ID=root;Password=Aragorn_2024;Server=127.0.0.1;Database=Aragorn;Port=3306;’)

What am I doing wrong, how can I get a working connection?

Thanks in advance.

Joop

I have tried, in both tracks, any combination of passwords, database names, usernames etc. No results.

2

Answers


  1. Chosen as BEST ANSWER

    In reply to Maurice

    Using MySql I use this code:

    import mysql.connector
    cnx = mysql.connector.connect(user='root', password='Aragorn_2024', 
    host='127.0.0.1',  database='Aragorn')
    cur = cnx.cursor()
    cur.execute("INSERT INTO sakila.tempora SELECT sysdate()")
    for row in cur.fetchall():
        print (row[0])
    cnxn.close())
    

    Using ODBC, the code looks like this: import pyodbc cnx = pyodbc.connect('DRIVER={Devart ODBC Driver for SQL Server};Server=127.0.0.1;Database=Aragorn;Port=3306;UserID=root;Password=Aragorn_2024') cur = cnx.cursor() cur.execute("INSERT INTO sakila.tempora SELECT sysdate()") for row in cur.fetchall(): print (row[0]) cnxn.close()


  2. In reply to shadow:

    Using the trail with MySql: I get no response at all. Python simply stops, and I have to start it up again from scratch.

    Using ODBC, I get:

    exec(open(‘pythonx.py’).read())
    1
    2
    Traceback (most recent call last):
    File "", line 1, in
    exec(open(‘pythonx.py’).read())
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "", line 25, in
    pyodbc.InterfaceError: (‘IM002’, ‘[IM002] [Microsoft][ODBC-stuurprogrammabeheer] De naam van de gegevensbron is niet gevonden en er is geen standaardstuurprogramma opgegeven (0) (SQLDriverConnect)’)

    Or, after adjusting the driver mentioned

    pyodbc.Error: (‘HY000’, ‘The driver did not supply an error!’)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search