python---pymysql

pymysql

import pymysql


class Mysql:
    def __init__(self):
        self.host = "192.168.0.1"
        self.port = 3306
        self.user = "root"
        self.password = "123456"
        self.db = "pymysql"
        self.charset = "utf8"
        self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.password,
                                    db=self.db, charset=self.charset)
        self.cursor = self.conn.cursor()

    def Create_Table(self, sql):
        self.cursor.execute(sql)
        self.conn.commit()
        self.cursor.close()
        self.conn.close()

    def Select(self, sql):
        self.cursor.execute(sql)
        results = self.cursor.fetchall()
        for row in results:
            print(row)  # 打印每行结果
        self.conn.commit()
        self.cursor.close()
        self.conn.close()

    def Insert(self, sql, *data):
        for sql_data in data:
            self.cursor.executemany(sql, sql_data)
        self.conn.commit()
        self.cursor.close()
        self.conn.close()

    def Update_or_Delete(self, sql):
        self.cursor.execute(sql)
        self.conn.commit()
        self.cursor.close()
        self.conn.close()


# 查看数据
# sql = "INSERT INTO departments( dep_id, dep_name) VALUES( %s, %s)"
# data = [(6, '运维部'), (7, '人事部')]


sql = "DELETE FROM departments WHERE dep_id=6;"

Mysql().Create_Table(sql)
Mysql().Select(sql)
Mysql().Insert(sql, data)
Mysql().Update_or_Delete(sql)


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值