Python实战之pymysql模块实战

上一篇:mysql摘要之常用操作实战 点击跳转
目录篇:python相关目录篇 点击跳转
下一篇:Python实战之sqlalchemy模块 and ORM介绍 点击跳转

linux and window 安装pymysql模块

安装:pip3 install pymysql

__author__ = "Burgess Zheng"

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pymysql

# 创建连接
conn = pymysql.connect(host='10.0.0.150', port=3306, user='python', passwd='123', db='oldboydb')

# 创建游标
cursor = conn.cursor()

'''
# 执行SQL,并返回收影响行数(查取)
effect_row = cursor.execute("select * from student") #执行该命令
print(cursor.fetchone())   #fetchone  一条一条数据取
print(cursor.fetchone())   #fetchone  一条一条数据取
#print(cursor.fetchmany(3))  #fetchmany 获取前N行数据
print('----------')
print(cursor.fetchall()) #fetchall 取所有数据 (由于前面取了2条,取剩下的
'''

data = [
    ("N1","2017-05-21","M"),
    ("N2","2017-06-22","F"),
    ("N3","2017-07-23","M"),
]

#执行SQL,并返回受影响行数(增删改)
effect_row = cursor.executemany("insert into student (name,register_date,gender) values(%s,%s,%s)",data)


# #默认开启事务,所以需要提交,不然无法保存新建或者修改的数据
conn.commit()

# 关闭游标
cursor.close()
# 关闭连接
conn.close()   

上一篇:mysql摘要之常用操作实战 点击跳转
目录篇:python相关目录篇 点击跳转
下一篇:Python实战之sqlalchemy模块 and ORM介绍 点击跳转​​​​​​​

 

你可能感兴趣的:(Python篇)