多测师拱墅校区肖sir_高级金牌讲师_python+pymysql

python+pymysql

=================================
python操作数据库
一、介绍pymysql
python操作mysql
pymysql 库名, python中的第三方库

二、pymysql下载安装
(1)方法1:
dos下 pip install pymysql 或 pip3 install pymysql
在这里插入图片描述

(2)在pycharm中setting 下载

在这里插入图片描述
======================================在这里插入图片描述

三、打开数据库:
在linux中打开数据库

在这里插入图片描述

四、实战:
1、导入pymysql
2.连接数据库
(1)连接数据库方法:
方法一:pymysql.Connection 大写
方法二:pymysql.connect 小写
(2)连接的参数:
host :主机 ip
user: 用户名 root
password或passwd :密码 123456
db或database :数据库 库名 dcs
port :端口 3306
charset : 编码格式 ‘utf8’
案例:
db=pymysql.connect(host=“192.168.157.129”,user=“root”,password=‘123456’,db=‘dcs’ ,port=3306, charset=‘utf8’)

(3)创建游标对象
游标对象=db.cursor()#创建游标对象
(4)yb.execute(语句) 执行游标:通过sql语句
(5)

a、fetchone()显示第一行
案例:print(yb.fetchone()) #显示第一行数据
b、.fetchmany(数据) 显示具体的行数
print(yb.fetchmany(2))#根据数字显示第数据
c、fetchall() 显示所有的内容
print(yb.fetchall()) #显示所有内容

案例:
import pymysql
db=pymysql.Connection(host=“192.168.157.129”,user=“root”,password=‘123456’,db=‘dcs’ ,port=3306,
charset=‘utf8’)
yb=db.cursor()#创建游标对象
sql=“select * from emp”
yb.execute(sql)

print(yb.fetchone()) #显示第一行数据
print(yb.fetchmany(2))#根据数字显示第数据
print(yb.fetchall()) #显示所有内容
案例2:通过遍历可以将所有数据打印出来
import pymysql
db=pymysql.connect(host=“192.168.157.129”,user=“root”,password=‘123456’,db=‘dcs’ ,port=3306,
charset=‘utf8’)
yb=db.cursor()#创建游标对象
sql=“select * from emp”
yb.execute(sql)
sy=yb.fetchall() #显示所有内容
for i in sy:
print(i)

==============================================
删除数据:
delete from 表 where 条件 语句

import pymysql
db=pymysql.connect(host=“192.168.157.129”,user=“root”,password=‘123456’,db=‘dcs’ ,port=3306,
charset=‘utf8’)
yb=db.cursor()#创建游标对象
sql="delete from emp where sid=1789 "
yb.execute(sql)

=============================
修改数据:
import pymysql
db=pymysql.connect(host=“192.168.157.129”,user=“root”,password=‘123456’,db=‘dcs’ ,port=3306,
charset=‘utf8’)
yb=db.cursor()#创建游标对象
sql="UPDATE emp set name=‘xiaoniu’ where sid=1776 "
yb.execute(sql)

=============================
插入数据
import pymysql
db=pymysql.connect(host=“192.168.157.129”,user=“root”,password=‘123456’,db=‘dcs’ ,port=3306,
charset=‘utf8’)
yb=db.cursor()#创建游标对象
sql=" INSERT into emp(sid) VALUES(100)"
yb.execute(sql)

=============================

封装数据库

import pymysql
class Db_Gs(object):
def init(self ,host,user,passwd,db,port):
self.host=host
self.user=user
self.passwd=passwd
self.db=db
self.port=port
def lianjie(self):
db=pymysql.connect(host=self.host,user=self.user,passwd=self.passwd,db=self.db,port=self.port,charset=‘utf8’)
return db
def one(self,sql):
db=self.lianjie()
yb=db.cursor()
yb.execute(sql)
cxone=yb.fetchone()
#print(cxone)
return cxone
def many(self,sql):
db=self.lianjie()
yb=db.cursor()
yb.execute(sql)
cxmany=yb.fetchmany(size=2)
return cxmany
def all(self,sql):
db=self.lianjie()
yb=db.cursor()
yb.execute(sql)
cxall=yb.fetchall()
return cxall
if name == ‘main’:
d=Db_Gs(host=“192.168.157.129”,user=“root”,passwd=“123456”,db=‘dcs’ ,port=3306)
print(d.one(‘select * from emp’))
print(d.many(‘select * from emp’))
print(d.all(‘select * from emp’))

======================================

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

多测师软件测试培训师肖sir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值