0

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.

1 Answer 1

3

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)"""
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way to insert the table in stack overflow? I tried your method and get the error message "column "dt" of relation "tweets" does not exist", do I have to rename it?
never mind I am just really retarded. it works now, thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.