多测师肖sir_高级金牌讲师_python+pymyql数据库
pyhton+pymysql
python操作数据库
一、下载安装pymysql
方法1: pip install pymysql
方法2:在pycharm 中搜索pymysql下载
方法1案如图:
方法2:如下图
二、pymysql的使用
(1)导入pymysql(import pymysql)
(2)就是打开虚拟机,开启数据库,获取连接数据
(3)连接数据库
import pymysql
db=pymysql.Connection(host=“192.168.157.136”,user=“root”,password=“123456”,
database=“aa”,port=3306)
host:主机名
user:用户
password或passwd :数据库密码
database :数据库
port:端口号
charset:utf-8
(4)创建一个游标cursor()
案例1:fetchone 游标获取第一行数据
import pymysql
db=pymysql.Connection(host=“192.168.157.136”,user=“root”,password=“123456”,
database=“aa”,port=3306,charset=“utf8”)
yb=db.cursor()#创建一个游标
sql=“select * from dept”
yb.execute(sql)
one=yb.fetchone() #fetchone()游标获取第一行数据,
print(one)
案例2:通过游标获取所有的数据
import pymysql
db=pymysql.Connection(host=“192.168.157.136”,user=“root”,password=“123456”,
database=“aa”,port=3306,charset=“utf8”)
yb=db.cursor()#创建一个游标
sql=“select * from dept”
yb.execute(sql)
all=yb.fetchall() #fetchall# ()游标获取所有数据,
print(all)
案例3:fetchmany 获取部分数据 size 是数量
import pymysql
db=pymysql.Connection(host=“192.168.157.136”,user=“root”,password=“123456”,
database=“aa”,port=3306,charset=“utf8”)
yb=db.cursor()#创建一个游标
sql=“select * from dept”
yb.execute(sql)
many=yb.fetchmany(size=3) #fetchmany# ()游标获取所有数据,
print(many)
===================================================
数据库中:
查:
import pymysql
db=pymysql.Connection(host=“192.168.157.136”,user=“root”,password=“123456”,
database=“aa”,port=3306,charset=“utf8”)
yb=db.cursor()#创建一个游标
sql=“select * from dept”
yb.execute(sql)
many=yb.fetchmany(size=3) #fetchmany# ()游标获取所有数据,
print(many)
删:
import pymysql
db=pymysql.Connection(host=“192.168.157.136”,user=“root”,password=“123456”,
database=“aa”,port=3306,charset=“utf8”)
yb=db.cursor()#创建一个游标
sql="delete from dept where dept1=104 "
yb.execute(sql)
增:
import pymysql
db=pymysql.Connection(host=“192.168.157.136”,user=“root”,password=“123456”,
database=“aa”,port=3306,charset=“utf8”)
yb=db.cursor()#创建一个游标
sql="insert into dept values (‘104’,‘dcs’) "
yb.execute(sql)
改:
import pymysql
db=pymysql.Connection(host=“192.168.157.136”,user=“root”,password=“123456”,
database=“aa”,port=3306,charset=“utf8”)
yb=db.cursor()#创建一个游标
sql=“update dept set dept_name=‘hz’ where dept1=‘104’”
yb.execute(sql)
for 循环的查看方式:
import pymysql
db=pymysql.Connection(host=“192.168.157.136”,user=“root”,password=“123456”,
database=“aa”,port=3306,charset=“utf8”)
yb=db.cursor()#创建一个游标
sql=“select * from dept”
yb.execute(sql)
all=yb.fetchall() #fetchall# ()游标获取所有数据,
for i in all:
print(i)
====================================
封装pymysql:
# import pymysql
# class Db(object):
# def __init__(self,host,user,password,database, port):
# self.host=host
# self.user=user
# self.password=password
# self.database=database
# self.port=port
# def lj(self):
# ljsjk=pymysql.connect(
# host=self.host,user=self.user,password=self.password,database=self.database,port=self.port,charset='utf8' )
# return ljsjk
# def one(self,sql):
# bl=self.lj()
# yb=bl.cursor()
# yb.execute(sql)
# print(yb.fetchone())
# def many(self,sql,x):
# bl = self.lj()
# yb = bl.cursor()
# yb.execute(sql)
# print(yb.fetchmany(x))
# def all(self, sql):
# bl = self.lj()
# yb = bl.cursor()
# yb.execute(sql)
# print(yb.fetchall())
# if __name__ == '__main__':
# dx=Db(host="192.168.157.134",user="root",password="Xlh1314*",database="dcs",port=3306)
# # dx.one("select * from aa")
# # dx.many("select * from aa",3)
# dx.all("select * from dept")