I would like to parametize the columns and my dataframe in an cursor.execute function. I'm using pymssql, because I like the fact that I can name the parametized columns. Yet I still don't know how to properly tell python that I'm referring to a specific dataframe and I would like to use this columns. Here is the last part of my code (I already tested the connection to my database etc. and it works):
with conn:
cursor = conn.cursor()
cursor.execute("""insert into [dbo].[testdb] (day, revenue) values (%(day)s, %(revenue)s)""", dataframe)
result = cursor.fetchall()
for row in result:
print(list(row))
I'm getting this error:
ValueError Traceback (most recent call last)
<ipython-input-52-037e289ce76e> in <module>
10 with conn:
11 cursor = conn.cursor()
---> 12 cursor.execute("""insert into [dbo].[testdb] (day, revenue) values (%(day)s, %(revenue)s""", dataframe)
13 result= cursor.fetchall()
14
src\pymssql.pyx in pymssql.Cursor.execute()
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1477 def __nonzero__(self):
1478 raise ValueError(
-> 1479 f"The truth value of a {type(self).__name__} is ambiguous. "
1480 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1481 )
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I figured out what my problem was. Due to the fact that I converted my dataframe to a dictionary (used dataframe.to_dict'index') the dictionary was a nested dict. I referred it with dataframe[0]
and it worked. Just in case someone had the same problem.