2017-02-22 66 views
2

我是新來的Python寫了這個簡單的代碼將數據插入到SQL服務器:PyPyODBC不插入記錄到MS-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服務器,沒有插入記錄。發生了什麼?我怎麼解決這個問題?

+1

您需要爲cursorlsmit()調整光標以反映! –

+0

可能像所有SQL數據庫:在關閉連接之前提交。 – polku

回答

5

我相信你還必須致電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() 
+0

5分鐘後接受你的答案 –

+0

@chertchertopert,Thankn你,很樂意幫助 – apomene

+1

@chertchertopert upvoting現在可以:) –