Frevelman
Frevelman

Reputation: 536

Timestamp - Not all arguments converted during string formatting psycopg2

I am currently trying to write a query that includes a timestamp when executing it.

My database looks like this:

  1. ID - 2. text_data - 3. userid - 4 time_stamp (datatype is "timestamp with timezone")

My query looks like this:

try:
    postgres_insert_query = """INSERT INTO tweets (text, userid) VALUES (%s, %s,) (dt,)"""
    insertion_data = (text, id, dt)
    cursor.execute(postgres_insert_query, insertion_data)
    connection.commit()

except (Exception, psycopg2.Error) as error:
    print("failed", error)

finally:
    if connection:
        cursor.close()
        connection.close()
        print("closed")

However, when I run it I get the error message "not all arguments converted during string formatting"

What am I missing here?

Edit: I got a question ban because my questions are not upvoted, but answered and some good discussion happens in the comments. But the stack exchange team told me to edit my questions to making them more clear, but they received answers so I think they were clear. I am not a native speaker so my English is not the best and I cant express myself that good. But they told me to edit them so they appear as new and can be upvoted. I don't see why that makes sense, but maybe it lifts my question ban.

Upvotes: 0

Views: 117

Answers (1)

mfitzp
mfitzp

Reputation: 15545

You have 3 parameters for the string, but only two string interpolation markers.

postgres_insert_query = """INSERT INTO tweets (text, userid) VALUES (%s, %s,) (dt,)"""

There are only two %s placeholders, but you then try to add 3 values.

insertion_data = (text, id, dt)

Perhaps you want (difficult to know without seeing your table).

postgres_insert_query = """INSERT INTO tweets (text, userid, dt) VALUES (%s, %s, %s)"""

Upvotes: 3

Related Questions