我是新来的蟒蛇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