擅长:python、mysql、java
<p>请试试这个</p>
<pre><code>db = MySQLdb.connect("localhost","root","aqw","PFE_Project" )
cursor = db.cursor()
sql = "SELECT * FROM ServerComponents"
response = []
try:
cursor.execute(sql)
results = cursor.fetchall()
nbre_row = cursor.rowcount
for row in results:
local_dict = {}
server_id = row[0]
local_dict["Server Name"] = 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)
local_dict["Server Price"] = p
response.append(local_dict)
except:
print "Error: unable to fetch data"
print(response)
</code></pre>
<p>我们不是附加到列表中,而是创建本地dict并将该本地dict添加到响应中。你知道吗</p>