import pymysql.cursors
"""
存储数据到MySQL
1,通过pip安装pymysql pip install pymysql
2,通过安装文件
python setup.py install
3.#引入开发包
import pymysql.cursors
4.#获取数据库链接
connection = pymysql.connect(
host='localhost',
user='root',password='123456',
db='wikiurl',charset='utf8mb4')
5.#获取会话指针
connection.cursor()
6.#执行SQL语句
cursor.execute(sql,(参数1,参数n))
7.#提交
connection.commit)
8.#关闭
connection.close()
"""
# 创建数据库连接
db = connection = pymysql.connect(
host="localhost",
user='root',
password="admin",
db="test2",
port=3306,
charset="utf8mb4"
)
name = {}
age = []
def test():
for i in range(20):
print(i)
name[str(i)] = i
# 获取会话指针
with db.cursor() as cursor:
sql = "insert into `test2`(`name`,`age`) values(%s,%s)"
try:
# 执行sql语句
cursor.execute(sql, (str(name[str(i)]), i))
# 提交事务
connection.commit()
except Exception as e:
print(e)
finally:
# 关闭资源
cursor.close()
# 关闭会话
connection.close()
if __name__ == '__main__':
test()
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- nryq.cn 版权所有 赣ICP备2024042798号-6
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务