[英]SELECT statement is MySql returning wrong value using python
I am trying to retrieve a value from a table stored in Mysql database using python(pycharm).我正在尝试使用 python(pycharm) 从存储在 Mysql 数据库中的表中检索一个值。 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.当我不断添加行时,output 会更改为行数,但从不返回存储的值。
The command works from MySql perfectly.该命令从 MySql 完美运行。
SELECT
update_id
FROM
telegram;
Output:233
^This is the correct output. ^这是正确的 output。 Why is this happening?为什么会这样? What changes should I make in my python code?我应该对我的 python 代码进行哪些更改?
According to the documentation on pymysql, this is how you are supposed to do the print out:根据 pymysql 上的文档,这就是你应该如何进行打印:
sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
cursor.execute(sql, ('webmaster@python.org',))
result = cursor.fetchone()
print(result)
You are missing "cursor.fetchone()"您缺少“cursor.fetchone()”
I hope that helps我希望这会有所帮助
According to the documentation , cursor.execute() returns the number of affected rows.根据 文档, cursor.execute() 返回受影响的行数。 You then need to fetch the content with fetch() or fetchall().然后您需要使用 fetch() 或 fetchall() 获取内容。 See the example at PyMySQL .请参阅PyMySQL中的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.