Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)

I am having two files. The first one has a connection and the getting of data. I am importing mysql.connector. This file is called tasksSql.py

def get_users():
    import mysql.connector

    con = mysql.connector.connect(user='****', password='*****',
                                  host='127.0.0.1',
                                  database='tasks')
    c = con.cursor()

    users = []    
    c.execute("""SELECT * FROM task_user""")

    for row in c:
        user = {
            'id': row[0],
            'first': row[1],
            'last': row[2],
            'email': row[3],
            'password': row[4],
            'creation_date': row[5]
        }
        users.append(user)
    c.close()
    return users

When I am running this file singly it works and returns the data.

Another file is named tasks.py where I am going to be importing this file, however, this is not working! When I import the file, it gives me the following error:

ImportError: No module named mysql.connector

Where am I doing wrong?

1 Answer

0 votes
by (12.7k points)

It depends on your version of python and how you had installed it, it is most likely that the MySQL connector wouldn't have been installed, you can install it using pip.

Execute the below command in order to install the MySQL connector:

pip install mysql-connector-python

Interested in SQL ? Check out this SQL Certification by Intellipaat.

If you want to know more about MySQL, refer to the below MySQL tutorial video that will help you out in a better way:

Related questions

0 votes
1 answer
asked Nov 29, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...