Search code examples
pythonpymssql

Is it possible to write a python list into a database with pymssql?


I am tyring to write a python list via pymssql into a database table. I am aiming to write each list entry into a different row of the same column.

When trying this I am getting the error:

ValueError: 'params' arg () can be only a tuple or a dictionary.

Is there a way with pymssql or should I use something else?

My code:

from bs4 import BeautifulSoup as bs
import re
import pandas as pd
from collections.abc import Iterable
import pymssql



conn = pymssql.connect(
    host='x',
    port=x,
    user='x',
    password='x',
    database='x'
)
cursor = conn.cursor() 
cursor.execute('SELECT x FROM x')

text = cursor.fetchall()

conn.close()



raw = []  
raw.append(text)
raw1 = str(raw)
soup = bs(raw1, 'html.parser')
autor = soup.get_text()

clear = []
s = autor.replace('\\n', '')
clear.append(s)




conn = pymssql.connect(
    host='x',
    port=x,
    user='x',
    password='x',
    database='x'
)

cursor = conn.cursor()

cursor.execute('INSERT INTO mytablename (columnname) VALUES (?);', [','.join(clear)])

conn.close()

Solution

  • You can use executemany:

    cursor.executemany('INSERT INTO mytablename (columnname) VALUES (%s);', clear)