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

一.安装PyMySQL库

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

python连接MySQL数据库,增删改查,批量增加_第1张图片

 

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

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

python连接MySQL数据库,增删改查,批量增加_第2张图片

二.连接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()     # 关闭数据库连接

你可能感兴趣的:(python,数据库,pycharm,mysql)