0
我和20列的表,我用這個代碼來獲得特定電影的各個領域作爲詞典:Python的mysql.connector - 正確的方式以檢索行作爲字典
import mysql.connector
def getMovie(id):
movie = {}
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
query = ('SELECT * FROM movies WHERE id = %s') % id
cursor.execute(query)
for row in cursor:
movie['id'] = row[0]
movie['actors'] = row[1]
movie['title'] = row[2]
# and so on for 20 lines
return movie
列名將是字典鍵。是否有更簡單的方法來歸檔相同的結果? 20行變量真的很糟糕....