Using Python to read and write in a SQL Server database

Below, I provide a snippet to read data from an Azure SQL Server database. Subsequently, it is written to another table in the same database.

The trick here is to read the data, store them in a matrix which will be written out to a second table. Elements within the matrix can be accessed with results[i][j].

import pyodbc 
conn = pyodbc.connect('DSN=AzureSQL;UID=tomvanmaanen;PWD=********')
data = []
cursor = conn.cursor()
cursor.execute('SELECT [CODE],[DESCRIPTION],[START_DATE],[END_DATE] FROM STAGINGZONE.T01_brp_country_code')
columns = [column[0] for column in cursor.description]
results = [columns] + [row for row in cursor.fetchall()]
for x in range(len(results)):
   Sql_insert_query = "INSERT INTO dbo.ff (test) VALUES ('" + results[x][1].replace("'", " ") + "')"
   cursor.execute(Sql_insert_query)
conn.commit()
cursor.close()

Door tom