OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

TypeError: Params must be in a list, tuple, or Row. SQL database data insertion

  • Thread starter Thread starter Juan Basilio de la Torre
  • Start date Start date
J

Juan Basilio de la Torre

Guest
I have a Cloud Azure DB where I want to introduce some data from another csv files. I am doing this with Python.

Basically, I manually store in a json fie the equivalencies between my DB columns and the csv columns in this format:

Code:
{
    "csv_column_1": {
        "table": "table1",
        "db_column": "column1"
    },
    "csv_column_2": {
        "table": "table1",
        "db_column": "column2"
}
}

That way, I can iterate through it in a loop and insert it into my database with the following code.

Code:
# Establish a connection to your SQL Server
conn = odbc.connect(connection_string)

# Create a cursor
cursor = conn.cursor()

for table, column, row in zip(tables, db_columns, data): 

    sqlinsert = f"INSERT INTO {table} ({column}) VALUES (?)"
    
    cursor.executemany(sqlinsert, row)
    cursor.commit()
    
print('Data inserted successfully.')

I have tried a lot of different codes and combinations and ways but no one works. I always get the error about TypeError: Params must be in a list, tuple, or Row which makes no sense cause my data is a list.

I have been able to introduce it but in a wrong way, columns are completely disorganized and it just appends rows while I want to keep the row level of my csv (basically inserting whole columns without minding about rows).
<p>I have a Cloud Azure DB where I want to introduce some data from another csv files. I am doing this with Python.</p>
<p>Basically, I manually store in a json fie the equivalencies between my DB columns and the csv columns in this format:</p>
<pre><code>{
"csv_column_1": {
"table": "table1",
"db_column": "column1"
},
"csv_column_2": {
"table": "table1",
"db_column": "column2"
}
}
</code></pre>
<p>That way, I can iterate through it in a loop and insert it into my database with the following code.</p>
<pre class="lang-py prettyprint-override"><code># Establish a connection to your SQL Server
conn = odbc.connect(connection_string)

# Create a cursor
cursor = conn.cursor()

for table, column, row in zip(tables, db_columns, data):

sqlinsert = f"INSERT INTO {table} ({column}) VALUES (?)"

cursor.executemany(sqlinsert, row)
cursor.commit()

print('Data inserted successfully.')
</code></pre>
<p>I have tried a lot of different codes and combinations and ways but no one works. I always get the error about <code>TypeError: Params must be in a list, tuple, or Row</code> which makes no sense cause my data is a list.</p>
<p>I have been able to introduce it but in a wrong way, columns are completely disorganized and it just appends rows while I want to keep the row level of my csv (basically inserting whole columns without minding about rows).</p>
 

Latest posts

Top