0

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
  • Could you elaborate on what 'number of rows' mean? What that number is, and what is the total number of records in that table? Commented Jul 11, 2019 at 9:21

1 Answer 1

2

Did you read the documentation? You need to fetch the results after executing: fetchone(), fetchall() or something like this:

import json
import pymysql

connection = pymysql.connect(user = "root", password = "", host = "127.0.0.1", port = "", database = "my_database")
with connection.cursor(pymysql.cursors.DictCursor) as cursor:
    cursor.execute("SELECT * FROM my_database.my_table")
    rows = cursor.fetchall()
    for row in rows:
        print(row)

You probably also want a DictCursor as the results are then parsed as dict.

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

Comments

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.