时间:2021-07-01 10:21:17 帮助过:14人阅读
)
# 使用cursor()方法获取操作游标
cur = conn.cursor()
# 使用execute方法执行SQL语句
data = cur.execute("select id,phone,num,name from test.user_info;")
# print data
info = cur.fetchmany(data)
cur.close()
conn.commit()
conn.close()
def dd(info):
s = ""
for i in info:
#print i[0]
#print i[1]
#print i[2]
#print i[3].decode('utf-8')
# 汉字需要 decode 成utf-8
s += '%s;%s;%s;%s#' % ( i[0],i[1],i[2],i[3].decode('utf-8') )
#print s
return s
# print(dd(info))
# print(date)
# 发送邮件
msg_from='123456@QQ.com' #发送方邮箱
passwd='XXXXXX' #填入发送方邮箱的授权码
msg_to='789@qq.com' #收件人邮箱
subject="python邮件测试 + date "#主题
content= str(dd(info)) #正文
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = msg_to
try:
s = smtplib.SMTP_SSL("smtp.exmail.qq.com",465)#邮件服务器及端口号
s.login(msg_from, passwd)
s.sendmail(msg_from, msg_to, msg.as_string())
print "发送成功"
except s.SMTPException,e:
print "发送失败"
finally:
s.quit()
=====================================
系统安装MySQL-python-1.2.5
下载 :https://pypi.python.org/pypi/MySQL-python
# yum install –y mysql-devel
安装:
[root@www soft]# unzip MySQL-python-1.2.5.zip
[root@www soft]# cd MySQL-python-1.2.5
[root@www soft]# python setup.py build
[root@www soft]# python setup.py install
测试:
[root@www soft]# python
>>> import MySQLdb
>>>
安装完毕。
人生第一个python脚本:从数据库查询数据并发送邮件
标签:pac 一个 test mysq fetch host string imp decode