我是新來的蟒蛇pymysql, (我紅寶石Mysql2寶石之前使用),我想拿到鑰匙,並從MySQL表中的值,並做一些動作:Python的pymysql - 遍歷MySQL表鍵和值
例如:
dbconnection = pymysql.connect(host=mysql_hostname, user=mysql_username, password=mysql_pass, db=mysql_schema, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
cursor = dbconnection.cursor()
_SQL = ("""
select * ...
""")
cursor.execute(_SQL)
result = cursor.fetchall()
for row in result:
print(row)
print("\n")
# How can I access each key and each value for example: STATUS is the value and 3 is the key
# I want to do something like this:
#'if the value of 'CAP' > 1: change the value of status where the ID key
# cursor.execute("UPDATE <table> SET row[STATUS]='1' WHERE ID='row[ID]'")
輸出:
{'STATUS': 3, 'ID': 10, 'CAP': 1}
{'STATUS': 3, 'ID': 11, 'CAP': 2}
{'STATUS': 3, 'ID': 12, 'CAP': 3}
由於
你是什麼意思,表**的鍵和值**?一個表可以有多個列。 –
我更新了我的問題,我希望能夠訪問每個結果的密鑰名稱和值名稱 – Berlin