Python PYODBC未将记录插入MS-SQL server

Python PYODBC未将记录插入MS-SQL server,python,sql-server,pypyodbc,Python,Sql Server,Pypyodbc,我是Python新手,编写了将数据插入SQL server的简单代码: 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

我是Python新手,编写了将数据插入SQL server的简单代码:

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 server时,没有插入任何记录。怎么搞的?如何解决此问题?

我相信您还必须调用
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()

您需要执行
cursor.commit()
,以反映更改!可能和所有SQL数据库一样:在关闭连接之前提交。@chertchertopert,谢谢,很高兴能帮助您享受新的投票特权:)@apomene从数据库noob获得免费的向上投票(下次我知道后会将其标记为重复)@Jean Françoisfare,谢谢,我非常感谢