Python数据库(二)-Mysql数据库插入数据
通过python连接mysql数据库,并插入数据
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import pymysql conn = pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='root',db='niushop_b2c') cursor = conn.cursor() data = [ ('John','abcdef'), ('Jack','123456'), ('Jane','abc123'), ] cursor.executemany("insert into sys_user (user_name,user_password) values(%s,%s)",data) #user_name,user_password为对应的字段名 conn.commit() #提交,不然不能保存增加的或者修改的数据 cursor.close() conn.close()
运行后,在mysql里查看一下结果
数据插入成功