Python自学第十二周(2)

女生的网名这么多〃 提交于 2019-11-27 04:59:42
python与linux下mysql交互:
记得关闭防火墙 service iptables stop、
 
 
 1 #Author: Zachary
 2 import pymysql
 3 # 创建连接
 4 conn = pymysql.connect(host='192.168.86.10', port=3306, user='root', passwd='112590', db='zach')
 5 # 创建游标\相当于命令行中的光标
 6 cursor = conn.cursor()
 7 # 执行SQL,并返回收影响行数
 8 effect_row = cursor.execute("select * from student")
 9 print(effect_row)
10 print("=======================================")
11 print(cursor.fetchone())
12 print(cursor.fetchone())
13 print(cursor.fetchone())
 
 
 
如果是
 1 #Author: Zachary
 2 import pymysql
 3 # 创建连接
 4 conn = pymysql.connect(host='192.168.86.10', port=3306, user='root', passwd='112590', db='zach')
 5 # 创建游标\相当于命令行中的光标
 6 cursor = conn.cursor()
 7 # 执行SQL,并返回收影响行数
 8 effect_row = cursor.execute("select * from student")
 9 print(effect_row)
10 print("=======================================")
11 print(cursor.fetchall())
 
 
 
 
 1 #Author: Zachary
 2 import pymysql
 3 # 创建连接
 4 conn = pymysql.connect(host='192.168.86.10', port=3306, user='root', passwd='112590', db='zach')
 5 # 创建游标\相当于命令行中的光标
 6 cursor = conn.cursor()
 7 data = [
 8     ("N1",22,"2019-09-10"),
 9     ("N2", 21, "2019-09-08"),
10     ("N3", 21, "2019-09-09"),
11 ]
12 cursor.executemany("insert into student(name,age,register_date) VALUE (%s,%s,%s)" ,data)
13 conn.commit()        #执行插入的时候必须要提交一下,但我试了,不用非得加
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!