Reputation: 185
Iteration of each record then print by key display nothing
#!/usr/bin/env python
# Print necessary headers.
print("Content-Type: text/html")
print()
import pymysql.cursors
# Connect to the database.
import pymysql
conn = pymysql.connect(db='candidate', user='root',passwd='123',host='localhost')
print ("connect successful!!")
c = conn.cursor()
c.execute("SELECT can_id,can_name,state,total_votes FROM voting")
result_set = c.fetchall()
for row in result_set:
print(row["can_id"])
print(row) works fine
Upvotes: 0
Views: 1452
Reputation: 715
I propose some changes using the PyMySQL documentation as reference:
#!/usr/bin/env python
# -*- conding: utf-8 -*-
# Print necessary headers.
print("Content-Type: text/html")
import pymysql.cursors
# Connect to the database.
# That is important: the order of connection parameters
# 1st host, 2nd user, ...
conn = pymysql.connect(
host='localhost'
user='root',
password='123',
db='candidate',
cursorclass=pymysql.cursors.DictCursor # <-- declare cursor class
)
print ("connect successful!!")
try:
with conn.cursor() as c:
query = "SELECT can_id, can_name, state, total_votes FROM voting;"
c.execute(query)
with c as pointer:
for row in pointer.fetchall():
print("Name: %s" % row['can_name'])
finally:
conn.close()
Upvotes: 0
Reputation: 1940
c = conn.cursor(pymysql.cursors.DictCursor)
or
conn = pymysql.connect(db='candidate', user='root', passwd='123', host='localhost', cursorclass=pymysql.cursors.DictCursor)
Upvotes: 2