2

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?

shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
chert chertopert
  • 197
  • 3
  • 14

1 Answers1

7

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()
apomene
  • 14,282
  • 9
  • 46
  • 72