这是一个非常非常古老的帖子,我知道 - 但我有同样的问题并且遇到了它。我开始工作了,我想我会为那些像我一样在这里绊倒的人分享。请善待,这是我第一次通过代码。它不漂亮,但它应该让你开始。
def exec_query(self, query_type, query):
# query_type: This is an enumumeration defining the CRUD operation of the query.
# Note, the native client (11.0) might change with time and Windows updates)
# Note, the driver varlue would normally go in a constant, I put it here just for clarity
with pypyodbc.connect('DRIVER={SQL Server Native Client 11.0}; ' + self.cnx_str) as connection:
cursor = connection.cursor()
cursor.execute(query)
if query_type is QueryType.create:
try:
cursor.execute("SELECT SCOPE_IDENTITY()")
row = cursor.fetchone()
seed_id = row[0]
except AttributeError:
seed_id = 0
cursor.commit()
return seed_id
# ... Additional code for non 'create' operations