如何在Python中使用存储过程将值插入到SQL Server表中

huangapple go评论129阅读模式
英文:

How to insert values into SQL Server table using Stored procedure in Python

问题

我是pytds的新手。在向SQL Server插入数据时遇到了TypeError: not enough arguments for format string的问题。

我遇到的问题是:

追踪回溯(最近的调用最后):
文件 "c:/Users/mydesk/Desktop/test/test_pytds.py",第 64 行,在
connect_and_call_stored_procedure(query, val)
文件 "c:/Users/mydesk/Desktop/test/test_pytds.py",第 48 行,在 call_sproc 中
cursor.execute(query, (tvp,))
文件 "C:\Program Files (x86)\Python38-32\lib\site-packages\pytds_init_.py",第 739 行,在 execute 中
self.execute(operation, params)
文件 "C:\Program Files (x86)\Python38-32\lib\site-packages\pytds_init
.py",第 701 行,在 _execute 中
operation = operation % names[0]
TypeError: not enough arguments for format string

我漏掉了什么。请提供您宝贵的意见。

我的完整代码:

  1. import pytds
  2. def get_query() -> str:
  3. sql = '''
  4. DECLARE @tbl [dbo].[tbl]
  5. insert into @tbl
  6. (
  7. col1, col2, col3
  8. )
  9. values (%s,%s,%s)
  10. '''
  11. query = '''
  12. EXECUTE [dbo].[stproc]
  13. @tbl
  14. '''
  15. return sql + query
  16. def call_sproc(query, val):
  17. server = 'my_server'
  18. user = 'my_user'
  19. password = 'secret'
  20. database = 'my_db'
  21. try:
  22. conn = pytds.connect(server=server, user=user, password=password, database=database)
  23. cursor = conn.cursor()
  24. tvp = pytds.TableValuedParam(type_name='dbo.tbl', rows=val)
  25. cursor.execute(query, (tvp,))
  26. conn.commit()
  27. cursor.close()
  28. conn.close()
  29. except pytds.Error as e:
  30. print("Connection issue:", e)
  31. val = [('col1_val'), ('col2_val'), ('col3_val')]
  32. my_sql = get_query()
  33. call_sproc(my_sql, val)
英文:

I am new to pytds. Facing TypeError: not enough arguments for format string issue while inserting data into SQL Server.

Issue I am getting is:
> Traceback (most recent call last):
> File "c:/Users/mydesk/Desktop/test/test_pytds.py", line 64, in <module>
> connect_and_call_stored_procedure(query, val)
> File "c:/Users/mydesk/Desktop/test/test_pytds.py", line 48, in call_sproc
> cursor.execute(query, (tvp,))
> File "C:\Program Files (x86)\Python38-32\lib\site-packages\pytds_init_.py", line 739, in execute
> self.execute(operation, params)
> File "C:\Program Files (x86)\Python38-32\lib\site-packages\pytds_init
.py", line 701, in _execute
> operation = operation % names[0]
> TypeError: not enough arguments for format string

Something I missed. Let me know your valuable inputs.

My complete code:

  1. import pytds
  2. def get_query() -&gt; str:
  3. sql = &#39;&#39;&#39;
  4. DECLARE @tbl [dbo].[tbl]
  5. insert into @tbl
  6. (
  7. col1, col2, col3
  8. )
  9. values (%s,%s,%s)
  10. &#39;&#39;&#39;
  11. query += &#39;&#39;&#39;
  12. EXECUTE [dbo].[stproc]
  13. @tbl
  14. &#39;&#39;&#39;
  15. return sql
  16. def call_sproc(query, val):
  17. server = &#39;my_server&#39;
  18. user = &#39;my_user&#39;
  19. password = &#39;secret&#39;
  20. database = &#39;my_db&#39;
  21. try:
  22. conn = pytds.connect(server=server, user=user, password=password, database=database)
  23. cursor = conn.cursor()
  24. tvp = pytds.TableValuedParam(type_name=&#39;dbo.tbl&#39;, rows=val)
  25. cursor.execute(query, (tvp,))
  26. conn.commit()
  27. cursor.close()
  28. conn.close()
  29. except pytds.Error as e:
  30. print(&quot;Connection issue:&quot;, e)
  31. ###
  32. val=[(&#39;col1_val&#39;), (&#39;col2_val&#39;), (&#39;col3_val&#39;)]
  33. my_sql = get_query()
  34. call_sproc(my_sql, val)

答案1

得分: -1

调用存储过程时,无需任何查询或DECLARE。只需按照文档中的说明使用callproc即可。

  1. cursor.callproc("dbo.stproc", (tvp,))

或者,如果您真的想使用临时批处理,则可以执行以下操作:

  1. cursor.execute("EXECUTE dbo.stproc %s", (tvp,))
英文:

To call a stored procedure, you don't need any query or DECLARE. Just do what the docs say and use callproc

  1. cursor.callproc(&quot;dbo.stproc&quot;, (tvp,))

Or if you really want to use an ad-hoc batch then do

  1. cursor.execute(&quot;EXECUTE dbo.stproc %s&quot;, (tvp,))

huangapple
  • 本文由 发表于 2023年8月9日 18:41:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定