0

I have read a similar issue (AttributeError: 'tuple' object has no attribute 'keys') and Psycopg's official documentation on placeholders for variables, but I am still getting stuck on what seems to be a most simple query. Sorry for asking this , but what's wrong with the various iterations I have tried?

    cur.execute('''
    UPDATE scrape_log 
    SET date_added=%s, result_count=%s 
    WHERE start_date=%s AND end_date=%s
    ''', date_scraped, result_count, start_date, end_date)

It returned AttributeError: 'tuple' object has no attribute 'keys'.

I also tried the following, but it returned the same error.

    cur.execute('''
    UPDATE scrape_log 
    SET VALUES (%s, %s)
    WHERE start_date=(%s) AND end_date=(%s)
    ''', (date_scraped, result_count), (start_date,), (end_date,))

What did I do wrong?

Louis
  • 63
  • 5

4 Answers4

0

Try this:

   cur.execute('''
    UPDATE scrape_log 
    SET date_added=%s, result_count=%s 
    WHERE start_date=%s AND end_date=%s
    ''', (date_scraped, result_count, start_date, end_date))

Enlico
  • 23,259
  • 6
  • 48
  • 102
Bjarni Ragnarsson
  • 1,731
  • 1
  • 6
  • 8
0

Parameters should be enclosed in a tuple, so try this:

cur.execute('''
    UPDATE scrape_log 
    SET date_added=%s, result_count=%s 
    WHERE start_date=%s AND end_date=%s
    ''', (date_scraped, result_count, start_date, end_date))
szrg
  • 79
  • 5
0
 query = """ UPDATE books
                SET title = %s
                WHERE id = %s """

    data = (title, book_id)

 cursor = conn.cursor()
        cursor.execute(query, data)
  • This would be a better answer if you explained how the code you provided answers the question. – pppery Jun 23 '20 at 00:21
0

Using your query is something lije this

query = "UPDATE scrape_log SET date_added=%s, result_count=%s 
    WHERE start_date=%s AND end_date=%s"

  cur.execute(query, (date_scraped, result_count, start_date, end_date,))