0

I am connecting a Python program in Visual Studio Code to a SQL databse stored in MySQL Workbench 8.0. I am using the PyMySQL connector to do this. However, I am running into an error with code that I have used from another question that I have posted. Here is the link and the code:

How do I connect a Python program in Visual Studio Code to MySQL Workbench 8.0?

pip install PyMySQL


import pymysql

con = pymysql.Connect(
    host='localhost',
    port=3306,
    user='root',
    password='123456',
    db='test',
    charset='utf8'
)

cur = con.cursor()

sql1 = 'select * from student'
cur.execute(sql1)
data = cur.fetchall()
cur.close()
con.close()

for i in data:
    print(str(i))

code screenshot

Here is a screenshot with my code and the error that I received.

I tried the code that I recieved from my previous question, but it resulted in an another error. I am pretty sure I have copied the code correctly and the database details. I have researched the error but have been unable to find its relevance to connecting Python programs to MYSQL Workbench 8.0 with PyMySQL.

6
  • Why not show your own code? The code in the picture is not the same as the code you pasted as text. And pip install pymysql is a command, it's not a code. Commented Nov 9, 2022 at 1:40
  • Is your database name and table name the same? Why are there no parentheses after your fetchall method? Is this what you mean by copying the code correctly? Commented Nov 9, 2022 at 1:45
  • Has your mysql modified the port number? Why is the port number 33060 in the code in the picture? Commented Nov 9, 2022 at 1:46
  • @JialeDu watched a video on setting up the connection in MySQL Workbench and it recommended using the port 33060 because it has a stronger connection. This is the video that it said it in. Commented Nov 9, 2022 at 8:00
  • Otherwise thank you for pointing out the typos. I will fix them now and see whether the program runs smoothly. Commented Nov 9, 2022 at 8:02

1 Answer 1

1

First of all, obviously you didn't copy the code in the answer correctly.

Your code has the following errors (only from the picture, I don't know what your complete code looks like)

  • The port number is 3306. NOT 33060. Of course, if you make changes when you install the database, you need to change it to the port number you use.

  • The fetchall method in data = cur.fetchall does parentheses. It shoud be data = cur.fetchall().

At the moment it seems that the error in the picture is due to the port number.

Modifying to the correct port number will remove this error.

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

3 Comments

What's your mistake? What does your code look like now? What error are you getting?
Thank you for the advice. I put this connection into my main program and have had some errors in that. I have posted that in another question. It would be helpful if you could answer that. stackoverflow.com/questions/74372581/…
What about this question? What you're doing is confusing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.