'How to solve this Python error when inserting list in database?
I'm trying to insert into database a list (that I extracted from a file) with 3 columns:
- log_time
- log_type
- message
I used this code:
#database connection
try:
#Connection object for connection to azure sql database
print( "Connecting to database..." )
params = urllib.parse.quote_plus(
'Driver=%s;' % driver +
'Server=tcp:%s,1433;' % server +
'Database=%s;' % database +
'Uid=%s;' % username +
'Pwd={%s};' % password +
'Encrypt=yes;' +
'TrustServerCertificate=no;' +
'Connection Timeout=30;')
conn_str = 'mssql+pyodbc:///?odbc_connect=' + params
global engine
engine = create_engine(conn_str,echo=False, fast_executemany=True)
except:
print('Cannot connect to destination database. Database is not available or connection information is incorrect. Please, review.')
with open("test.log",'r') as data_file:
for line in data_file:
values = [line.rstrip('\n').split("\\") for line in data_file]
final_list = [ list(filter(lambda l: l != '', inside_l)) for inside_l in values]
print(final_list)
query = 'INSERT INTO control (log_time, log_type, message) VALUES (%s, %s, %s);'
engine.execute(query, final_list)
Yet, when I execute the script it gives me this error:
... displaying 10 of 15 total bound parameter sets ...
By the way, when I print the list, is something like this:
['2022-04-28 22:33:07,470', 'DEBUG', 'Files concatenation'], ...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
