0

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'

Shadow
  • 33,525
  • 10
  • 51
  • 64
GAURAV PAWAR
  • 33
  • 1
  • 9

2 Answers2

0

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

flutter_bee
  • 150
  • 9
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

GAURAV PAWAR
  • 33
  • 1
  • 9