Insert data in a database using pyodbc

  pyodbc, python, sql, sql-server

I made a code which gather datas from files and I want to put them in a database using pyodbc in python and SQL server express 2019.
Here is my code :

import pyodbc
conn = pyodbc.connect('Driver={SQL SERVER};'
                      'Server=<my server>;'
                      'Database=<my database>;'
                      'Trusted_Connection=yes;'

cursor=conn.cursor()
sql="""
INSERT INTO [dbo].[<my table>]
            ([data1]
            ,[data2]
            ,[data3]
            ,[data4])
     VALUES
            ('{0}'
            ,'{1}'
            ,'{2}'
            ,'{3}')
""".format(value1, value2, value3, value4)
cursor.execute(sql)

So, when I execute this SQL request in SQL Server Express 2019, it works and add a new line on the database, BUT ! If I execute my python code and try to enter my values in the database, it crashes and my database is now empty.

Please help me I don’t understand why it doesn’t work with python but does when I write it on the SQL Server Express

Source: Python Questions

LEAVE A COMMENT