Skip to content
Advertisement

TypeError: Params must be in a list, tuple, or Row in Python

I built out a python script the retrieves data from Auth0 and publish it to Ms-sql but im getting errors

 for d in data:
        print d["email"], d["created_at"],d["last_login"],d["last_ip"] #this part worked great
        SQLCommand = ("INSERT INTO [dbo].[Auth0_stg] "
                      "([Email],[created_at],[last_login],[last_ip]) "
                      " VALUES(?,?,?,?)")
        Values = d["email"],d['created_at'],d['last_login'],d['last_ip']

        cursor.executemany(SQLCommand,Values)
        cursor.commit()

When I did the print statements,

d['email'], d['last_login']

everything printed great. but when i used SQL commands to try to populate my table, it returns this error

File "C:Python27libsite-packagespypyodbc.py", line 1454, in execute
    raise TypeError("Params must be in a list, tuple, or Row")
TypeError: Params must be in a list, tuple, or Row

Any suggestions/insights appreciated!

Advertisement

Answer

executemany executes the same request several times with different parameters. Because of that it is expecting a sequence as second parameter (such as a list of list, or a list of tuples…)

In your code you only want to do a single request for each iteration of your for loop. Replace executemany by execute and it should work fine.

EDIT: For your second issue, before the line:

Values = d["email"],d['created_at'],d['last_login'],d['last_ip']

You can fill the empty values with a for loop:

for key in ["email", "created_at", "last_login", "last_ip"]:
    if key not in d:
        d[key] = ""

EDIT 2: To create a datetime object from a string, you can use strptime():

>>> from datetime import datetime
>>> my_str_date = "2016-10-18T20:15:45.454Z"
>>> my_datetime = datetime.strptime(my_str_date, "%Y-%m-%dT%H:%M:%S.%fZ")
>>> print(my_datetime)
2016-10-18 20:15:45.454000

You can then format it using strftime():

>>> print(my_datetime.strftime("%Y/%m/%d %H:%M:%S"))
2016/10/18 20:15:45
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement