3
import pyodbc
import csv
conn = pyodbc.connect('Driver=ODBC Driver 17 for SQL Server;'
                      'Server=SIS10647\MSSQLSERVER14;'
                      'Database=LeelaVenkatesh;'
                      'Trusted_Connection=yes;')

cursor = conn.cursor()

cursor.execute('''

               CREATE TABLE employee
               (
               empid int,
               Name nvarchar(50)
               )

               ''')

with open('C:\PYTHON\Textfiles/1.txt') as f:
    reader = csv.reader(f)
    for row in reader:
        cursor.execute("""INSERT INTO employee(empid,name)
                          VALUES(%s, %s)
                       """, row)
conn.commit()
cursor.close()
print("Done")

I am getting the following error

cursor.execute("""INSERT INTO employee(empid,name) pyodbc.ProgrammingError: ('The SQL contains 0 parameter markers, but 1 parameters were supplied', 'HY000')

Text file data looks like

empid|Name
1|'Ram'

2 Answers 2

1
cursor.execute("""INSERT INTO employee(empid,name)
                  VALUES(?, ?)
                  """, row)

and if this doesn't work, assuming row is an array, try this:

cursor.execute("""INSERT INTO employee(empid,name)
                  VALUES(?, ?)
                  """, row[0], row[1])
Sign up to request clarification or add additional context in comments.

9 Comments

IndexError: list index out of range this is the error iam getting
so what isthe structure of row? like is it an array, if so, is the length of the array fixed for all the rows?
rows are sperated by delimter |
could you give an example? Value of row
Added in question please look into it
|
0
empid|Name
1|'Ram'
try this
empid,Name
1,Ram
with 
cursor.execute("""INSERT INTO employee(empid,name)
                  VALUES(?, ?)
                  """, row[0], row[1])

1 Comment

Also the file must have only values without headers, seperated by comma

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.