Python mysql 打印为纯文本
Python mysql print to plain text
我目前正在尝试将信息从 MySQL 数据库打印到控制台,但它一直以元组而不是纯文本的形式打印。有人对此有解决方案吗?
我的代码:
sql = "SELECT stream_time FROM schedule_cet WHERE cet_day = 'monday'"
c.execute(sql)
monday_cet = c.fetchone()
print(monday_cet)
我已经尝试过 re
库并且替换在元组中不起作用。
默认情况下 mysql 游标 return 值作为元组,因为查询可能 return 多个字段
SELECT a, b, c FROM xx
(1, 2, 3)
SELECT a FROM xx
(1, )
如果您使用 fetchall
,您将得到一个元组列表。您可以使用 dict
而不是元组,通过在获取光标时使用 as_dictionary=True
,您将获得 {"stream_time" : "XX"}
的结果
您只需要访问第一项
print(monday_cet[0])
# or unpack it
monday_cet, = c.fetchone()
print(monday_cet)
我目前正在尝试将信息从 MySQL 数据库打印到控制台,但它一直以元组而不是纯文本的形式打印。有人对此有解决方案吗?
我的代码:
sql = "SELECT stream_time FROM schedule_cet WHERE cet_day = 'monday'"
c.execute(sql)
monday_cet = c.fetchone()
print(monday_cet)
我已经尝试过 re
库并且替换在元组中不起作用。
默认情况下 mysql 游标 return 值作为元组,因为查询可能 return 多个字段
SELECT a, b, c FROM xx
(1, 2, 3)
SELECT a FROM xx
(1, )
如果您使用 fetchall
,您将得到一个元组列表。您可以使用 dict
而不是元组,通过在获取光标时使用 as_dictionary=True
,您将获得 {"stream_time" : "XX"}
您只需要访问第一项
print(monday_cet[0])
# or unpack it
monday_cet, = c.fetchone()
print(monday_cet)