1

I am on windows 10 and using python3.9.

I installed the pagkage >python3 -m pip install mysql-connector-python

Now I try to run a simple program

import mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",
    database="pythonDB"
)
mycursor = mydb.cursor()


mycursor.execute("SELECT * FORM customers")

myresult = mycursor.fetchall()

for x in myresult:
    print(x)

I am getting the following error when running >python3 select.py

Traceback (most recent call last): File "G:\Python_w3school\mysql\select.py", line 1, in import mysql.connector File "C:\Users\pawar\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\mysql\connector_init_.py", line 42, in import dns.resolver File "C:\Users\pawar\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\dns\resolver.py", line 20, in import socket File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\socket.py", line 54, in import os, sys, io, selectors File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\selectors.py", line 12, in import select File "G:\Python_w3school\mysql\select.py", line 3, in mydb = mysql.connector.connect( AttributeError: module 'mysql' has no attribute 'connector'

1
  • Does it need to do anything with python3?? Commented May 28, 2021 at 7:16

2 Answers 2

0

Seems like the Python3 is not taking SQL directly inside the execute function. Assign the query to a variable and pass it to the execute() function.

It worked for me!!!

sql = "SELECT * FROM customers"
mycursor.execute(sql)

Also try try renaming the file if it is something like select.py to select_customers.py

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

1 Comment

i have error not just while executing the select sql but also while establisghing a connection.
-1

try these:

AttributeError: module 'mysql' has no attribute 'connector'

Attribute error:module 'mysql' has no attribute 'connector'

basically change the import to

from mysql import connector

1 Comment

from mysql import connector also dont work.

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.