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介绍 点击跳转​​​​​​​

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值