I'm new to Python and wrote this simple code for inserting data to the 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()
The code executes fine and even me Insert Finish!
, but when I check SQL server, there are no records inserted. What happened? How can I solve this problem?
I believe you must also call connect.commit()
. Try:
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()