栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python连接MySQL数据库,增删改查,批量增加

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python连接MySQL数据库,增删改查,批量增加

一.安装PyMySQL库

1、Pip install pymysql 常规安装---因我已经安装过,所以报错:检查最新版本的pip时出错。

 

2、可以直接在pycharm里面直接进行搜索下载

路径如下:File——Settings——Project下的Python Interpreter,点击+号,输入需要下载的库,点击Install Package.

二.连接MySQL---进行增删改查

import pymysql

#连接数据库
connection = pymysql.connect(
            host="host",              #主机号
            port=3306,                # 端口号
            user="root",              #用户名
            passwd="password",        #密码
            db="Test",                #数据库名
            charset="utf8")

cursor = connection.cursor() #获取游标

sql="insert into table001(name,age,sex) values('张三','177','男')"  #增加数据---增
cursor.execute(sql)   #运行
connection.commit()   #提交数据,不然数据库中会没有增加的数据
print("增加数据成功")


sql="alter table table001 drop sex"      #删除一列数据---删
cursor.execute(sql)
print("删除数据成功")


sql="update table001 set age=99 where ID=2"  #修改单个数据---改
cursor.execute(sql)   #运行
connection.commit()   #提交数据,不然数据库中会没有增加的数据
print("修改数据成功")


sql="select * from table001"     #查询数据---查
cursor.execute(sql)             #运行
result = cursor.fetchall()      #使用 fetchall() 方法获取所有数据
print(result)

# -------关闭数据库
cursor.close()  # 关闭游标
connection.close()  # 关闭数据库连接

三.批量增加数据

import pymysql

#连接数据库
connection = pymysql.connect(
            host="host",              #主机号
            port=3306,                # 端口号
            user="root",              #用户名
            passwd="password",        #密码
            db="Test",                #数据库名
            charset="utf8")

cursor = connection.cursor() #获取游标

#批量增加数据
nameage = [("李四", 36, "男"), ("王五", 17, "女"), ("麻子", 29, "男")]      
sql = "insert into table001(name,age,sex) values(%s,%s,%s)"  # %s 占位符
for i in nameage:
    cursor.execute(sql,(i[0],i[1],i[2]))  #运行
    connection.commit()    #提交数据,不然数据库中会没有增加的数据
print("批量增加数据成功")

四.进行封装

每个人的方法不同,可以按照你自己的方法进行封装

import pymysql

class mysql:
    def __init__(self):
        self.connection = pymysql.connect(
            host="host",                    #主机号
            port=3306,                      # 端口号
            user="root",                    #用户名
            passwd="password",              #密码
            db="Test",                      #数据库名
            charset="utf8")

    def sent(self,sql):
        try:
            cursor = self.connection.cursor()
            if sql.split()[0].lower() == "select":  #判断是否是查询语句
                cursor.execute(sql)                 #执行语句
                result = cursor.fetchall()          #使用 fetchall() 方法获取所有数据
                print(result)                       
            else:
                cursor.execute(sql)            #执行语句       
                self.connection.commit()       #提交数据,不然数据库中会没有增加的数据
                print("运行成功")
            cursor.close()
        except Exception as e:
            self.connection.rollback()      # 执行回滚操作
            print(e)
        finally:
            self.connection.commit()    # 关闭游标
            self.connection.close()     # 关闭数据库连接

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1009793.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号