我是python的新手,尝试使用下面给出的简单程序是一个试图从表中获取数据并显示它的程序。安装了Python3.4和mysql.connector 2.0.4,以http://localhost:804/cgi-bin/ex7.py的身份在localhost中运行
它正在连接数据库,但未从表中获取数据
#!"C:\python34\python.exe"
import sys
import mysql.connector
print("Content-Type: text/html;charset=utf-8")
print()
conn = mysql.connector.connect(host='localhost',port='8051',
database='example',
user='root',
password='tiger')
cursor = conn.cursor()
if conn.is_connected():
print('Connected to MySQL database')
cursor.execute(""" SELECT * FROM album """)
for row in cursor:
print (row[1])
输出为:
连接到MySQL数据库
不从表中打印数据
请指出哪里出了问题
最佳答案
我想你错过了这一部分
conn = mysql.connector.MySQLConnection(host='localhost',port='8051',
database='example',
user='root',
password='tiger')
cursor = conn.cursor()
if conn.is_connected():
print('Connected to MySQL database')
cursor.execute(""" SELECT * FROM album """)
# fetch all of the rows from the query
data = cursor.fetchall ()
# print the rows
for row in data :
print row[1]