python连接SQL SERVER数据库

#coding=utf-8
import pymssql
import win__intertion
import OpenVPN_interaction
import nc_interaction
class MSSQL:
    def __init__(self,load,user,password,database):
        self.load=load
        self.user=user
        self.password=password
        self.database=database

    def get_connet(self):
        if not self.database:
            raise(NameError,'请先设置数据库信息')

        self.conn=pymssql.connect(host=self.load,user=self.user,password=self.password,database=self.database)
        cur=self.conn.cursor()                                                                                           #执行游标
        if not cur:
            raise (NameError,'连接数据库失败')
        else:
            return cur                                                                                                   #如果连接上就返回conn.cursor()

    def get_execute(self, sql):                                                                                          #执行的是查询语句(select)
        cur=self.get_connet()
        cur.execute(sql)                                                                                                #执行游标
        reslist=cur.fetchall()                                                                                          #返回所有结果、多个元组
        self.conn.close()
        return reslist                                                                                                  #返回于元组与列表的形式

    def get_Noexcute(self,sql):                                                                                         #执行的是非查询语句(delete,update,insert)
        cur=self.get_connet()
        cur.exeute(sql)
        self.conn.commit()
        self.conn.close()                                                                                                #非查询语句不需要返回

def main():
    sqq = MSSQL('192.168.2.245', 'sa', '123@abc', 'AIS20220825114138')
    sql1=sqq.get_execute("select FShortNumber from t_Item where fshortnumber='6810AA002483'")
    sql2=[]
    if sql1==sql2:
        print('没有数据')
        OpenVPN_interaction.get_pid('open.exe')
        nc_interaction.getnctertion()
        nc_interaction.NC_interaction()

    else:
        list1=sql1[0]
        list2=list1[0]
        print('已有数据物料简码为:',list2)
        OpenVPN_interaction.get_pid('open.exe')
        nc_interaction.getnctertion()
        nc_interaction.NC_interaction()


if __name__ == '__main__':
    win__intertion.winintertion()
    main()

你可能感兴趣的:(数据库,python,开发语言)