首页 > 数据库 >python: pymssql stored procedures insert output

python: pymssql stored procedures insert output

时间:2023-06-17 19:34:39浏览次数:46  
标签:insert iobject python xxx cursor stored pymssql output conn

sql script:

IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Insert_BookKindOut')
DROP PROCEDURE proc_Insert_BookKindOut
GO
CREATE PROCEDURE proc_Insert_InsuranceMoneyOut
(
    @InsuranceName NVarChar(1000),
    @InsuranceCost float,
    @IMonth Int,
    @ID Int output
)
AS
Begin
INSERT INTO InsuranceMoney
(
    [InsuranceName] ,
    [InsuranceCost],
    [IMonth]
)
    VALUES
(
    @InsuranceName,
    @InsuranceCost,
    @IMonth
)
SELECT @ID=@@IDENTITY
END
GO

  

    def insertOutProc(self,iobject):
        """
        插入操作 返回值 存储过程 https://github.com/pymssql/pymssql/issues/441
        python pymssql stored procedures insert output geovindu
        param:iobject 输入保险类
        :return:
        """

        conn = pymssql.connect(
            server=self._strserver,
            user=self._struser,
            password=self._strpwd,
            database=self._strdatabase
        )
        cursor = conn.cursor()
        #cursor.callproc("")
        outid=pymssql.output(int)
        args=(iobject.getInsuranceName(), iobject.getInsuranceCost(), iobject.getIMonth(), outid)  #pymssql.output
        #cursor.execute(f"exec 存储过程名称 @参数1='xxx',@参数2='xxx',@参数3='xxx',@参数4='xxx'")
        newout=cursor.callproc("dbo.proc_Insert_InsuranceMoneyOut",args)
        print(newout[3])  #  ('医疗', 900, 3, 221)
        conn.commit()
        conn.close()
        return newout[3]

  

sql = """INSERT INTO [database].[file name].[table name] ([column1],[column2],[column3],[column4]...)
                                  VALUES({value1},{value2},'{value3}',{value4}...);
                                                """.format(
    value1=Parameter 1,
    value2=Parameter 2,
    value3=Parameter 3,
    value4=Parameter 3,
    ...)
cursor.execute(sql)
conn.commit()
conn.close()

import pymssql
"""Configuration"""
server='xxx'
user ='xxx'
password='xxx'
database='xxx'
"""Connect to the database"""
conn = pymssql.connect(server, user, password, database)
cursor = conn.cursor()
cursor.execute(f"exec stored procedure name @Parameter1='xxx',@Parameter2='xxx',@Parameter3='xxx',@Parameter4='xxx'")
result = cursor.fetchall()  #Get the result set
for i in result:
  print(i)  #Traverse and print the data of the query result set

  

标签:insert,iobject,python,xxx,cursor,stored,pymssql,output,conn
From: https://www.cnblogs.com/geovindu/p/17488098.html

相关文章

  • Day03 3.3 使用Python还原算法
    Day033.3使用Python还原算法加密分类1、单向加密:MD5、sha系列不可逆2、对称加密:AES、DES3、非对称加密:RSA、DSA4、补充算法:base64【一】md5importhashlibm=hashlib.md5()m.update('helloworld'.encode("utf8"))print(m.hexdigest())【二......
  • python: encode and decode
    importbinasciigeovin=b"geovindu"adu=base64.b64encode(geovin)#加密码print(adu)edu=base64.b64decode(adu)#解密print(edu)s=["医疗",400,1]column=('InsuranceName','InsuranceCost'......
  • PySide6(Qt for Python) QTableWidget表头边框线问题
    这个问题是在Windows10平台下特有问题。网络上有很多QtC++的解决方案。但是没有特定的PySide6的解决方案(以下是参考的QtC++的解决方案)。链接:https://blog.csdn.net/qq_22642239/article/details/122863344问题描述C++的解决方案是设置纵横表头的样式表:horizontalHeader,v......
  • Python中eval 与 exec 函数的区别
    eval和exec都是Python内置函数,并且都能把字符串当作代码执行,那区别是什么呢?eval函数只能计算单个表达式的值,而exec函数可以动态运行代码段;exec函数能直接执行文件中的Python代码,eval函数不能实现这个功能;eval函数可以有返回值,而exec函数返回值永远为None;举......
  • Python潮流周刊#7:我讨厌用 asyncio
    你好,我是猫哥。这里记录每周值得分享的Python及通用技术内容,部分为英文,已在小标题注明。(标题取自其中一则分享,不代表全部内容都是该主题,特此声明。)首发于我的博客:https://pythoncat.top/posts/2023-06-17-weekly7......
  • Python如何爬取京东mac电脑的数据?全球HTTP代理应用
    618要来了,不买点啥总觉得少了点什么,正好我用了5 6 年的电脑想换,这可不就来活了嘛!我们用Python也能简单获取某东上的mac电脑价格。不整虚的,直接就是一个冲,来看代码:importrequestsfrombs4importBeautifulSoupdefget_jd_laptops():url="https://search.jd.com/Search......
  • python之shutil模块
    shutil可以简单的理解为sh+util,是对os模块的补充,主要针对文件的拷贝、删除、移动、压缩和解压缩等操作。1复制复制文件:importshutil#从src文件路径复制数据到dst,复制成功后返回dst完整路径,src、dst是文件路径不能是文件目录。如果当前的dst已存在的话就会被覆盖掉shuti......
  • Python 字符编码转换(转载)
    Python字符编码转换1.在python2默认编码是ASCII,python3里默认是unicode2.unicode分为utf-32(占4个字节),utf-16(占两个字节),utf-8(占1-4个字节),soutf-16就是现在最常用的unicode版本,不过在文件里存的还是utf-8,因为utf8省空间3.在py3中encode,在转码的同时......
  • Python3网络爬虫开发实战阅读笔记
    基本库的使用网络请求库urllib(HTTP/1.1)Python自带请求库,繁琐基础使用:略requests(HTTP/1.1)Python常用第三方请求库,便捷基础使用:略httpx(HTTP/2.0)Python第三方库,支持HTTP/2.0,支持异步请求,支持Python的async请求模式pipinstall'httpx[http2]'基础使用:与requests相似,默认......
  • python unitest 测试装置(Test Fixture)
    unittest框架提供了测试装置(TestFixture)的功能,用于在测试用例执行前后进行准备和清理操作。测试装置包括setUp()方法和tearDown()方法,分别用于在每个测试用例执行前和执行后进行相应的操作。下面是一个示例,演示如何使用测试装置:importunittestclassMyTestCase(unittest.Te......