NoyRaz
NoyRaz

Reputation: 80

Python Error When insert one column into SQL Server database

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')

Upvotes: 0

Views: 116

Answers (1)

NoyRaz
NoyRaz

Reputation: 80

Solve this by change the column name from "item" to "Item" maybe "item" its python saved word, not sure

Upvotes: 0

Related Questions