![](/img/trans.png)
[英]MySQL Stored Procedure DELETE not working when called from PYTHON
[英]SQL Server: stored procedure not working when called from python
我有一個名為Points
的簡單表,其中包含兩列QuestionID
和UserID
:
QuestionID UserID
-------------------
5 2
6 1
我寫了一個存儲過程來將數據插入到這個表中:
CREATE PROCEDURE [dbo].[SetPoints]
@QuestionID INT,
@UserID INT
AS
INSERT INTO dbo.Points (QuestionID, UserID)
VALUES (@QuestionID, @UserID)
我執行了這個程序,它似乎工作正常。
然后我寫了 Python 代碼來連接這個數據庫並執行這個過程。
import pyodbc as po
# Connection variables
server = '################'
database = '#############'
username = '#############'
password = '################'
# Connection string
cnxn = po.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=' +
server+';DATABASE='+database+';UID='+username+';PWD=' + password)
cursor = cnxn.cursor()
# Prepare the stored procedure execution script and parameter values
storedProc = "EXEC SetPoints @QuestionID = ?, @UserID = ?"
params = (2, 3)
# Execute stored procedure with parameters
cursor.execute(storedProc, params)
# Close the cursor and delete it
cursor.close()
del cursor
# Close the database connection
cnxn.close()
當我運行Execute
Python 代碼時沒有顯示錯誤,但是如果我檢查Points
表,則沒有插入任何內容....
有人可以告訴我發生了什么事嗎? 該過程在 SSMS 中運行良好,但是當我從 Python 執行它時,沒有任何反應。
解決方案:解決方案是將auto_commit=True
添加到pyodbc.connect()
function。 我從本網站的另一篇文章中找到了答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.