更新pyodbc下的更新语句问题。

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

update statement under pyodbc issue

问题

我目前正在开发一个Python程序,该程序与多个数据库进行交互。我正在使用pyodbc进行连接并执行查询。其中一个数据库是Azure数据库。我注意到有时候虽然程序成功运行且没有报错,但发送的数据在数据库中没有更新。是否有一些最佳实践我应该遵循,以确保这种情况不会发生,还是这与我的代码或数据库连接有关?我是一个初学者,感谢大家的帮助,谢谢!

此外,.commit() 方法是否应在每次运行SQL后执行?

该程序应该根据条件更新数据库中的一行数据,但有时该特定查询不起作用,但没有报错。我之后还执行了多个查询,下一个查询没有问题,成功执行。

该查询是一个简单的查询,如下所示:

UPDATE DraftVReg SET VRStatus = 'Potential Duplicate Found' WHERE RowID = ?
英文:

I am currently developing a program in python that interacts with multiple database. I am using pyodbc to connect, and execute queries. One of the database is an azure database. I noticed sometimes the sent data is not updated in the database although the program run successfully and no error was thrown. Is there any practices that i should follow to make sure this doesn't happen or is this related to my code or db connection issue? I am a beginner. Would appreciate everyone's help thank you!

Also is the .commit() line should be run after every sql run?

The program should be updating a row of data in the database based on a condition, this particular query sometimes doesn't take effect, but no error was thrown. I also executed multiple queries after that, no issue was found for the next queries. It is successfully executed.

the query is a simple query which is

UPDATE DraftVReg SET VRStatus = 'Potential Duplicate Found' WHERE RowID = ?

答案1

得分: 2

我尝试在我的环境中重现您的情况,并能够使用Pyodbc模块更新Azure SQL DB中的SQL行。

是的,在以编程方式执行诸如更新或插入等操作之后,非常有必要使用

conn.commit 

来提交对数据库的更改。

1) 使用Select语句获取数据。

在我尝试UPDATE语句之前,我能够成功地在pyodbc代码中使用Select * from 'Tablename'查询成功获取表格数据。

import pyodbc
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=tcp:sqlservernamesql.database.windows.net,1433;DATABASE=databasename;UID=siliconuser;PWD=Password;')
#conn.commit()
cursor = conn.cursor()
cursor.execute('Select * FROM StudentReviews')
#conn.commit()
for i in cursor:
    print(i)
cursor.close()
conn.close()

结果:-

更新pyodbc下的更新语句问题。

2) 更新行需要使用conn.commit()

代码:-

import pyodbc
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=tcp:siliconserversql.database.windows.net,1433;DATABASE=silicondb;UID=userid;PWD=Password;')
cursor = conn.cursor() 
#cursor.execute('Select * FROM StudentReviews')
cursor.execute("UPDATE StudentReviews SET ReviewTime = ('7') WHERE ReviewText = ('SQL DB')")
conn.commit()
cursor.close()
conn.close()

结果:-

成功执行更新语句,并在Azure SQL中更新了表格行,请参考下面的图像:

更新pyodbc下的更新语句问题。

3) 使用autocommit=true

感谢 @Gord thompson 的评论和建议!

代码:-

import pyodbc
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=tcp:siliconserversql.database.windows.net,1433;DATABASE=silicondb;UID=username;PWD=Password;', autocommit=True) 
#conn.commit()
cursor = conn.cursor()
cursor.execute("UPDATE StudentReviews SET ReviewTime = ('8') WHERE ReviewText = ('SQL DB')")
cursor.close()
conn.close()

结果:- 使用autocommit=true,您不需要每次更新SQL DB时都添加conn.commit。

更新pyodbc下的更新语句问题。

英文:

I tried to reproduce your scenario on my end and was able to update the SQL row in the Azure SQL DB with Pyodbc module.

Yes, Its very necessary to use

conn.commit 

to commit your changes inside a database after you perform operations such as Update or Insert inside Azure SQL DB programmatically.

1) Fetch Data with Select statement.

I was able to fetch the Table’s data successfully with Select * from ‘Tablename’ query inside pyodbc code before I try UPDATE statement.

import pyodbc
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};''SERVER=tcp:sqlservernamesql.database.windows.net,1433;''DATABASE=databasename; UID=siliconuser;PWD=Password;')
#conn.commit()
cursor = conn.cursor()
cursor.execute('Select * FROM StudentReviews')
#conn.commit()
for  i  in  cursor:
print(i)
cursor.close()
conn.close()

Result:-

更新pyodbc下的更新语句问题。

2) UPDATE the rows require conn.commit()

Code :-

import pyodbc
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};''SERVER=tcp:siliconserversql.database.windows.net,1433;''DATABASE=silicondb; UID=userid; PWD=Password;')
cursor = conn.cursor() 
#cursor.execute('Select * FROM StudentReviews')
cursor.execute("UPDATE StudentReviews SET ReviewTime = ('7') WHERE ReviewText = ('SQL DB')")
conn.commit()
cursor.close()
conn.close()

Result:-

Update statement Executed successfully and the Table Row was updated in Azure SQL, Refer Below :-

更新pyodbc下的更新语句问题。

3) With autocommit=true

Thank you @Gord thompson for the comment and suggestion!

Code :-

import pyodbc
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};''SERVER=tcp:siliconserversql.database.windows.net,1433;''DATABASE=silicondb; UID=username; PWD=Password;', autocommit=True) 
#conn.commit()
cursor = conn.cursor()
cursor.execute("UPDATE StudentReviews SET ReviewTime = ('8') WHERE ReviewText = ('SQL DB')")
cursor.close()
conn.close()

Results :- With autocommit=true, You do not need to add conn.commit everytime you update the SQL DB.

更新pyodbc下的更新语句问题。

huangapple
  • 本文由 发表于 2023年1月9日 17:51:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055533.html
匿名

发表评论

匿名网友

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

确定