python 3.7 Method for inserting data into sqlserver using pymssql

  • 2021-07-10 20:26:22
  • OfStack

python 3.7 Inserts data into sqlserver using pymssql


import pymssql
conn = pymssql.connect(host='szs',server='SZS\SQLEXPRESS',port='51091', user='python', password='python', database='python',charset='utf8',autocommit=True)
cur = conn.cursor()
sql = "insert into [novals] values ('python','python','python','python');COMMIT "
try:
  cur.execute(sql)
except:
  conn.rollback()
cur.close()
conn.close()

The insertion of the data requires COMMIT after the sql statement and the setting of autocommit=True at the connection. If not, I have tried the insertion unsuccessfully.

ps: Let's look at the pymysql write data failure problem

Add commit after sql statement


conn.execute("insert into ....; commit")

Summarize


Related articles: