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:
JavaScript
x
51
51
1
from bs4 import BeautifulSoup as bs
2
import re
3
import pandas as pd
4
from collections.abc import Iterable
5
import pymssql
6
7
8
9
conn = pymssql.connect(
10
host='x',
11
port=x,
12
user='x',
13
password='x',
14
database='x'
15
)
16
cursor = conn.cursor()
17
cursor.execute('SELECT x FROM x')
18
19
text = cursor.fetchall()
20
21
conn.close()
22
23
24
25
raw = []
26
raw.append(text)
27
raw1 = str(raw)
28
soup = bs(raw1, 'html.parser')
29
autor = soup.get_text()
30
31
clear = []
32
s = autor.replace('\n', '')
33
clear.append(s)
34
35
36
37
38
conn = pymssql.connect(
39
host='x',
40
port=x,
41
user='x',
42
password='x',
43
database='x'
44
)
45
46
cursor = conn.cursor()
47
48
cursor.execute('INSERT INTO mytablename (columnname) VALUES (?);', [','.join(clear)])
49
50
conn.close()
51
Advertisement
Answer
You can use executemany:
JavaScript
1
2
1
cursor.executemany('INSERT INTO mytablename (columnname) VALUES (%s);', clear)
2