Python Error When insert one column into SQL Server database

  database, python-3.x, sql-server

I’m trying to insert data from one column DataFrame into a SQL Server table.

This is my code:

import numpy as np
import pandas as pd
import time as t
import random as r
import datetime as dt
import mysql.connector
import pyodbc 
import sqlalchemy as sal
from sqlalchemy import create_engine

server = '*****'
database = '****'
username = '' 
password = ''


# --- define our connection string ---

sql = pyodbc.connect('DRIVER={SQL Server};
                    SERVER='+server+';
                    DATABASE='+database+';
                    Trusted_Connection =yes;')


# --- create the connection cursor ---
cursor = sql.cursor()

d = {
  'Item':[  'None',
  'eurnzd',
  'gbpusd',
  'nzdchf',
  'cadchf',
  'xtiusd',
  'xtiusd']
 }

df = pd.DataFrame(d)

for index, row in df.iterrows():
    cursor.execute("INSERT INTO test (item) VALUES(?)", 
                    (row.item)) 
    
sql.commit()
cursor.close()

I get this error:

ProgrammingError

Traceback (most recent call last)

in
40 for index, row in df.iterrows():
41 cursor.execute("INSERT INTO test (item) VALUES(?)",
—> 42 (row.item)) #———-Only this column causes problems
43
44

ProgrammingError: (‘Invalid parameter type. param-index=0 param-type=method’, ‘HY105’)

Source: Python-3x Questions

LEAVE A COMMENT