3.27-1 pymysql下载及使用

发布于:2025-03-31 ⋅ 阅读:(15) ⋅ 点赞:(0)

pymysql

1、下载pymysql 库(第三方机构)

方法一:dos下安装:

pip3  install   pymysql  或pip   install  pymysql

图片

图片

方法二:在pycharm中下载

图片

二.使用pymysql

(1)数据安装好,能连接

图片

(2)连接数据库

图片

图片

(1)连接方式:pymysql.Connection 或者pymysql.connect 

(2)包含内容

a.host 主机:填写IP地址

b.user  数据库用户名

c.password 或passwd  密码:

d.databases  或db  库名

e.port  端口  :默认3306

f.charset ='utf8'    编码格式

案例:

db=pymysql.Connection(host="192.168.157.128",user="root",password="123456",database="test",port=3306,charset='utf8')

图片

(2)将连接内容设置成一个变量,然后创建一个游标对象

db.cursor

(3)使用游标对象去执行sql语句

(4)在根据需要显示内容使用 fetchone,fetchall,fetchmany

代码:

import  pymysql #导入pymysql
lj=pymysql.connect(    host="192.168.1.210",user="root",passwd="123456",port=3306,
    database="hh",charset="utf8"
)  #pymysql连接参数
yb=lj.cursor() #创建游标对象
sql="select  * from  student" #sql语句
yb.execute(sql) #执行sql语句
# one=yb.fetchone() #查看执行一行数据
# print(one)
# many=yb.fetchmany(size=3)
# print(many)
all=yb.fetchall()
print(all)

二.pymysql操作数据库的增删改查

1.删除

import  pymysql #导入pymysql
lj=pymysql.connect(
    host="192.168.1.210",user="root",passwd="123456",port=3306,
    database="hh",charset="utf8"
)  #pymysql连接参数
yb=lj.cursor() #创建游标对象
#删除
sql="delete  from  student where id=1 "
yb.execute(sql)

2.插入数据

import  pymysql #导入pymysql
lj=pymysql.connect(
    host="192.168.1.210",user="root",passwd="123456",port=3306,
    database="hh",charset="utf8"
)  #pymysql连接参数
yb=lj.cursor() #创建游标对象
#插入数据:
sql="INSERT into  student(id)  VALUES (1)"
yb.execute(sql)

3.修改数据

import  pymysql #导入pymysql
lj=pymysql.connect(
    host="192.168.1.210",user="root",passwd="123456",port=3306,
    database="hh",charset="utf8"
)  #pymysql连接参数
yb=lj.cursor() #创建游标对象
#插入数据:
sql="UPDATE  student  set  id=30 where id=20;"
yb.execute(sql)

案例2:

import  pymysql #导入pymysql
lj=pymysql.connect(
    host="192.168.1.210",user="root",passwd="123456",port=3306,
    database="hh",charset="utf8"
)  #pymysql连接参数
yb=lj.cursor() #创建游标对象
#插入数据:
sql="UPDATE  student  set  id=40 where id=30;"
yb.execute(sql)
sql2="select  * from  student"
yb.execute(sql2)
print(yb.fetchall())

三.数据库封装

import pymysql
class  Sjk(object):
    def __init__(self,host,user,passwd,port,database):
        self.host=host
        self.user=user
        self.passwd=passwd
        self.port=port
        self.database=database
    def lj(self):
       ljsql= pymysql.connect(
            host=self.host,user=self.user,port=self.port,passwd=self.passwd,
            database=self.database,charset="utf8"
        )
       return ljsql
    def one(self,sql1):
        s=self.lj()
        yb=s.cursor()
        yb.execute(sql1)
        print(yb.fetchone())

    def many(self, sql2,x):
        s = self.lj()
        yb = s.cursor()
        yb.execute(sql2)
        print(yb.fetchmany(size=x))

    def all(self, sql3):
        s = self.lj()
        yb = s.cursor()
        yb.execute(sql3)
        # print(yb.fetchall())
        sy=yb.fetchall()
        for i  in sy:
            print (i)
if __name__ == '__main__':
    dx=Sjk(host="192.168.1.210",user="root",passwd="123456",port=3306,
     database="hh")
    # dx.one("select *  from student")
    # dx.many("select *  from student",3)
    dx.all("select *  from student")

在测试,你可以通过数据库造数据,删除数据,修改数据,

断言

1.查询后台数据

2.造数据

3.删除数据,让数据不冗余


网站公告

今日签到

点亮在社区的每一天
去签到