pudo / dataset

Easy-to-use data handling for SQL data stores with support for implicit table creation, bulk loading, and transactions.
https://dataset.readthedocs.org/
MIT License
4.76k stars 297 forks source link

Question about custom SQL queries #334

Closed yilu1015 closed 4 years ago

yilu1015 commented 4 years ago

I ran into a question when trying to use db.query to update some values. The code below returns AttributeError: 'tuple' object has no attribute 'keys', but an almost identical rendition in psycopg2 executes the command correctly. What could be the issue here?

        db = dataset.connect('postgresql://my_database')
        db = dataset.connect()
        db.begin()
        db.query('''
            UPDATE scrape_log 
            SET date_scraped=%s, result_count=%s 
            WHERE start_date=%s AND end_date=%s;
            ''', (date_scraped, result_count, start_date, end_date))
        db.commit()

vs.

        conn = psycopg2.connect('postgresql://my_database')
        cur = conn.cursor()
        cur.execute('''
            UPDATE scrape_log 
            SET date_scraped=%s, result_count=%s 
            WHERE start_date=%s AND end_date=%s;
            ''', (date_scraped, result_count, start_date, end_date))
        conn.commit()

Now if anyone could kindly let me know if there's a more elegant solution using update, I shall be very grateful. Sorry for what might be a sophomoric question, but for someone new to SQL, this library has made my life so much easier. Thanks so much!