英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论