# -*- coding: UTF-8 -*-
import MySQLdb
# 打开数据库连接
db = MySQLdb.connect("localhost", "root", "root", "sjzeis", charset='utf8' )
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# 使用execute方法执行SQL语句
sql = "select * from t_cms_channel"
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
for row in results:
channel_name = row[6]
channel_code = row[5]
print("code:" + channel_code + " name:" + channel_name)
# 关闭数据库连接
db.close()
运行结果: