不同类型的数据转变为str
import MySQLdb
con=MySQLdb.connect(host = '172.100.7.219',user = 'hexin',passwd='sat1028&THS',port= 3306,db='dss',charset='utf8')
cur = con.cursor()
cur.execute("select channel_name,alarm_type,alarm_date,alarm_stat from adm_log_alarm where alarm_type >='1' and (channel_name like '%%兴议%%' or channel_name like 'XY%%') limit 1".decode("utf8"))
1L
alldata = cur.fetchall()
print alldata
(('XY-\xe5\x8c\x97\xe9\x80\x9a\xe9\x81\x93\xe8\xa5\xbf', 3, datetime.datetime(2019, 11, 7, 10, 16, 18), 1),)
- 循环取值
for sql_info in alldata:
warn_id = sql_info[0].decode(“utf8”).encode(“gbk”).strip()
if sql_info[1] == 3:
warn_be = u’爱你一万年’
else:
warn_be = u’爱你1万年’
warn_date = sql_info[2].strftime(’%Y-%m-%d %H:%M:%S’).encode(“gbk”)
- 各个值之前类型和值
类型
type(sql_info[0])
<type 'str'>
type(sql_info[1])
<type 'int'>
type(sql_info[2])
<type 'datetime.datetime'>
值
print sql_info[0]
XY-北通道西
print sql_info[1]
3
print sql_info[2]
2019-11-07 10:16:18
各个值之后类型和值(标准格式为utf8,转为gbk后为乱码)
类型
type(warn_id)
<type 'str'>
type(warn_be)
<type 'str'>
type(warn_date)
<type 'str'>
值
print warn_id
XY-▒▒ͨ▒▒▒▒
print warn_be
▒▒▒▒һ▒▒▒▒
print warn_date
2019-11-07 10:16:18