我有一个数据库,我在其中注册了一些数据。我试图选择所有行并尝试将表格的每一行放入字典中,但我似乎无法做到这一点。
这是代码:
db = MySQLdb.connect("localhost","root","aqw","PFE_Project" )
cursor = db.cursor()
sql = "SELECT * FROM ServerComponents"
try:
cursor.execute(sql)
results = cursor.fetchall()
nbre_row = cursor.rowcount
server_name = []
server_price = []
for row in results:
server_id = row[0]
server_name.append(row[1])
core_number = int(row[2])
clock_speed = int(row[3])
ram = int(row[4])
rom = row[5]
hdd = int(row[6])
video_card = row[7]
cache_memory = row[8]
# calculation metric is a function that i used to calculate the server prize
p = calculations_metric (core_number, clock_speed, ram, hdd, video_card)
server_price.append(p)
try :
# i wanna the attribute "response" be iterative
response = {"Server Name" : server_name , "Server Price" : server_price }
except :
print "error in response"
except:
print "Error: unable to fetch data"
print(response)
这是我得到的结果:
{"Server Name": ["Dell1", "Dell2"], "Server Price": [149, 151]}
但我想看到的结果是这样的:
{"Server Name" : Dell1, "Server Price": 149}
{"Server Name" : Dell2, "Server Price": 151}
谁能帮我?