发送邮件和连接数据方法

发送邮件 

import yamail #上传到了pip源
# import yagmail #发附件的附件如果是中文名,是乱码

# yagmail
smtp = yamail.SMTP(
host='smtp.qq.com',#改成自己邮箱的邮箱的服务器即可
user='511402865@qq.com',
password='sdfsdf'#如果是163、qq等免费邮箱的话需要授权码,
# 自己公司的邮箱,一般都使用面膜
)

smtp.send(to=['511402865@qq.com'],#发送给谁
subject='你好,请查收附件',#邮件主题
cc=['623010336@qq.com',],#抄送,如果是多个人写list
contents='邮件正文',#邮件正文
attachments=['笔记.txt'] #附件,如果是多个附件,写list
)
smtp.close()

连接数据库

import pymysql

#oracle sqlserver ..

host = '118.24.3.40'
user = 'jxz'
password = '123456' #字符串
db='jxz'
port = 3306 #int类型

connect = pymysql.connect(host=host,user=user,
password=password,
port=port,db=db,
autocommit=True
)
cur = connect.cursor(pymysql.cursors.DictCursor) #建立游标,仓库管理员
pymysql.cursors.DictCursor 这行意思转成字典把
# cur.execute('insert into students values (38,"ljl","nan",19,"天马座","北京");')
# cur.execute('insert into students (name,class) values ("ljj2","天马座");')
# cur.execute('delete from students where id = 21;')
# cur.execute('update students set name="魏强2" where id=20;')

# connect.rollback()#回滚 回滚和提交互斥的,如果提交无法回滚,没有提交之前可以回滚 sql 执行,回滚就是撤销
# connect.commit()#提交
cur.execute('select * from students limit 5;')
print(cur.description) #表的描述
# cur.execute('select * from students where id=1')

# print('fetchmany',cur.fetchmany(2))
# print('fetchone',cur.fetchone())
print('fetchall',cur.fetchall())#拿到所有的结果

# for data in cur: 也可以循环数据
# print(data)

cur.close() 关闭游标关闭数据库
connect.close()
原文地址:https://www.cnblogs.com/weilemeizi/p/14520071.html