Import CSV file into SQL Server using Python
<p>I am having trouble uploading a CSV file into a table in MS SQL Server, The CSV file has 25 columns and the header has the same name as table in SQL which also has 25 columns. When I run the script it throws an error </p>

<pre><code>params arg (&lt;class 'list'&gt;) can be only a tuple or a dictionary
</code></pre>

<p>What is the best way to import this data into MS SQL? Both the CSV and SQL table have the exact same column names. </p>

<p>Here is the code:</p>

<pre><code>import csv
import pymssql

conn = pymssql.connect(
    server="xx.xxx.xx.90",
    port = 2433,
    user='SQLAdmin',
    password='xxxxxxxx',
    database='NasrWeb'
)

cursor = conn.cursor()
customer_data = csv.reader('cleanNVG.csv') #25 columns with same header as SQL

for row in customer_data:
    cursor.execute('INSERT INTO zzzOracle_Extract([Customer Name]\
      ,[Customer #]\
      ,[Account Name]\
      ,[Identifying Address Flag]\
      ,[Address1]\
      ,[Address2]\
      ,[Address3]\
      ,[Address4]\
      ,[City]\
      ,[County]\
      ,[State]\
      ,[Postal Code]\
      ,[Country]\
      ,[Category ]\
      ,[Class]\
      ,[Reference]\
      ,[Party Status]\
      ,[Address Status]\
      ,[Site Status]\
      ,[Ship To or Bill To]\
      ,[Default Warehouse]\
      ,[Default Order Type]\
      ,[Default Shipping Method]\
      ,[Optifacts Customer Number]\
      ,[Salesperson])''VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,)',row)

conn.commit()
cursor.close()
print("Done")
conn.close()
</code></pre>

<p>This is what the first rows of the CSV file looks like</p>

<p><a href="http://i.stack.imgur.com/NA0je.png" rel="nofollow"><img src="http://i.stack.imgur.com/NA0je.png" alt="enter image description here"></a></p>

<p>You are using <code>csv.reader</code> incorrectly. The first argument to <code>.reader</code> is not the path to the CSV file, it is</p>

<blockquote>
  <p>[an] object which supports the iterator protocol and returns a string each time its <code>__next__()</code> method is called â file objects and list objects are both suitable.</p>
</blockquote>

<p>Hence, according to the example in the <a href="https://docs.python.org/3/library/csv.html" rel="nofollow">documentation</a>, you should be doing something like this:</p>

<pre class="lang-python prettyprint-override"><code>import csv
with open('cleanNVG.csv', newline='') as csvfile:
    customer_data = csv.reader(csvfile)
    for row in customer_data:
        cursor.execute(sql, tuple(row))
</code></pre>

python python-3.x pymssql