Intellipaat Back

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

I have a MySQL database named my_database , and it that database there are a lot of tables. I want to connect MySQL with Python and to work with specific table named my_table from that database.

This is the code that I have for now:

import json

import pymysql

connection = pymysql.connect(user = "root", password = "", host = "127.0.0.1", port = "", database = "my_database")

cursor = connection.cursor()

print(cursor.execute("SELECT * FROM my_database.my_table"))

This code returns number of rows, but I want to get all columns and rows (all values from that table). I have also tried SELECT * FROM my_table but result is the same.

1 Answer

0 votes
by (25.1k points)

To get all the columns and rows returned by the execution of the query, you need to use  fetchall() methods.

add this at the end of your code.

cursor.execute("SELECT * FROM my_database.my_table")

rows = cursor.fetchall()

for row in rows:

    print(row)

Related questions

0 votes
1 answer
asked Dec 6, 2020 in SQL by Appu (6.1k points)
+3 votes
2 answers
0 votes
1 answer
0 votes
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...