PyPyODBC 没有向 MS-SQL 服务器插入记录

PyPyODBC not inserting record to MS-SQL server

我是 Python 的新手,写了这个简单的代码来将数据插入 SQL 服务器:

import pypyodbc
connect = pypyodbc.connect('Driver={Sql Server Native Client 11.0};Server=.;Database=SAMPLE;Trusted_Connection=yes;')
cursor = connect.cursor()
print('Trying to insert!')
cursor.execute("insert into [SAMPLE].[dbo].[Register] VALUES ('behzad','razzaqi')")
print('Insert Finish!')
connect.close()

代码执行正常,甚至我 Insert Finish!,但是当我检查 SQL 服务器时,没有插入任何记录。发生了什么?我该如何解决这个问题?

相信你也一定要打电话给connect.commit()。尝试:

import pypyodbc
connect = pypyodbc.connect('Driver={Sql Server Native Client 11.0};Server=.;Database=SAMPLE;Trusted_Connection=yes;')
cursor = connect.cursor()
print('Trying to insert!')
cursor.execute("insert into [SAMPLE].[dbo].[Register] VALUES ('behzad','razzaqi')")
connect.commit()
print('Insert Finish!')
connect.close()