Python Mysql查询未返回结果。

huangapple go评论60阅读模式
英文:

Python Mysql Queary not returning results

问题

我有一个数据库,当我查询一张表时,我得到了67个结果。SQL语句是:

SELECT lower(UserName) from Participants ;

我尝试连接到数据库,没有连接错误。

def db_connect ():
try:
cnx = mysql.connector.connect(user=db_user, password=db_password,host=db_host,database=db_name)
return cnx
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("你的用户名或密码有问题")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("数据库不存在")
else:
print(err)

def main():
cnx = db_connect()
cursor = cnx.cursor()
query = ("SELECT lower(UserName) from Participants ;")
cursor.execute(query)
print(cursor.rowcount)


<details>
<summary>英文:</summary>

I have a DB and when I query a table I get 67 results. The SQL is: 

SELECT lower(UserName) from Participants ;

I try to connect to the DB, and I get no connection errors.

def db_connect ():
try:
cnx = mysql.connector.connect(user=db_user, password=db_password,host=db_host,database=db_name)
return cnx
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)

def main():
cnx = db_connect()
cursor = cnx.cursor()
query = ("SELECT lower(UserName) from Participants ;")
cursor.execute(query)
print(cursor.rowcount)

It prints out -1 for rowcount. The connection to the DB appears to be working, the SQL is a simple query...

</details>


# 答案1
**得分**: 1

尝试在`print(cursor.rowcount)`之前添加`cursor.fetchall()`。

<details>
<summary>英文:</summary>

 - Try adding `cursor.fetchall()` before the `print(cursor.rowcount)` 

</details>



huangapple
  • 本文由 发表于 2023年2月8日 23:46:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定