python连接sqlserver,封装操作

1封装

# 导入Flask类
import pymssql
import traceback

class Mssql(object):
    # 连接库
    def base(database):
        connect = pymssql.connect(
            user='sa',
            password='123456',
            database=f'{database}',
            charset='utf8',
            as_dict=True
        )
        if connect:
            print("数据库连接成功!")
        else:
            print("链接失败!")
        return connect

    # 查询数据
    def query(connect,sql):
        try:
            cursor = connect.cursor()
            cursor.execute(sql)
            req = cursor.fetchall()
        except Exception:
            #打印错误信息
            traceback.print_exc()
            return "出错了"

        # print(res)
        return req


    # 新增,table表,data数据
    def inserts(connect, table, data):
        # 组装数据
        name = ''
        val = ''
        for v1 in data:
            if name == "":
                name = f"{v1}"
                val = f"'{data[v1]}'"
            else:
                name = f"{name},{v1}"
                val = f"{val},'{data[v1]}'"

        insert = f"INSERT INTO {table}({name}) VALUES({val})"
        print(insert)
        try:
            cursor = connect.cursor()
            cursor.execute(insert)
            connect.commit()
        except Exception:
            #打印错误信息
            traceback.print_exc()
            print(str(Exception))
            return "出错了"

        return 1

    # 修改,table表,data数据,where条件
    def updates(connect, table, data, where):
        # 组装数据
        sql = ''
        for v1 in data:
            if sql == "":
                sql = f"{v1}='{data[v1]}'"
            else:
                sql = f"{sql},{v1}='{data[v1]}'"
        update = f"UPDATE {table} set {sql} where {where}"
        print(update)
        try:
            cursor = connect.cursor()
            cursor.execute(update)
            connect.commit()
        except Exception:
            #打印错误信息
            traceback.print_exc()
            return "出错了"

        return 1

    #删除,table表,where条件
    def deletes(connect, table, where):
        delete = f"delete from {table} where {where}"
        print(delete)
        try:
            cursor = connect.cursor()
            cursor.execute(delete)
            connect.commit()
        except Exception:
            #打印错误信息
            traceback.print_exc()
            return "出错了"

        return 1

    # 关闭连接
    def close(conn):
        conn.close()

2使用

from configs.Mssql import Mssql
database = "SD65512_Sample"

def query():
    conn = Mssql.base(database)
    req = Mssql.query(conn, "SELECT billid FROM s_salechild")
    print(req)
    return 1

def add():
    conn = Mssql.base(database)
    data = {'id': 1}
    req = Mssql.inserts(conn, "dade", data)
    print(req)
    return 1

def update():
    conn = Mssql.base(database)
    data = {'dade': 888}
    Mssql.updates(conn, "dade", data, "id=1")
    return 1

def delete():
    conn = Mssql.base(database)
    Mssql.deletes(conn, "dade", "id=1")
    return 1

# 关闭连接
def close():
    conn = Mssql.base(database)
    conn.close()

# 测试
# query()
# add()
# update()
delete()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大得369

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值