1

I am trying to retrieve a value from a table stored in Mysql database using python(pycharm). But instead of outputting the stored value it outputs number of rows instead.

import pymysql
connection = pymysql.connect(host='localhost',
                             user='root',
                             password='passw',
                             database='database1',
                             charset='utf8',
                             port=3306
                             )
x=connection.cursor()
select = x.execute('''SELECT
    update_id
FROM
    telegram;
''')
print(select)

Output: 1 

^Wrong output(Output equals number of rows). As I keep adding on rows the output changes to the number of rows but never returns the value stored.

The command works from MySql perfectly.

SELECT
        update_id
    FROM
        telegram;
Output:233

^This is the correct output. Why is this happening? What changes should I make in my python code?

2
  • are you using the correct database?? Commented May 28, 2020 at 13:08
  • Yes sir , The UPDATE command works using python on the same database and table. Commented May 28, 2020 at 13:10

2 Answers 2

1

According to the documentation on pymysql, this is how you are supposed to do the print out:

    sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
    cursor.execute(sql, ('[email protected]',))
    result = cursor.fetchone()
    print(result)

enter image description here

You are missing "cursor.fetchone()"

I hope that helps

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

Comments

0

According to the documentation, cursor.execute() returns the number of affected rows. You then need to fetch the content with fetch() or fetchall(). See the example at PyMySQL.

1 Comment

If you find one of the answers satisfactory, mark that answer "accepted" with the check mark. It helps both you and me!

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.