简体   繁体   中英

Multiple parameters psycopg2 where clause - Syntax error

I get a syntax error at or near the WHERE clause in the last query below.

The query immediately before it with only one parameter works fine, so I am guessing the error has to do with the fact that I am trying to pass two parameters.

Thanks in advance.

cur.execute('''SELECT street_name, id FROM tablename 
            WHERE (((suburb = '') IS NOT FALSE) 
            AND ((street_name2 = '') IS NOT FALSE));''')

datesfrompdf = cur.fetchall()
for rowdate, rowid in datesfrompdf:
    cur.execute("DELETE FROM tablename WHERE id = %s;", (rowid + 1,)  #this works fine

    cur.execute('''INSERT INTO tablename (got_date)
                VALUES (%s) WHERE ((suburb IS NOT NULL)     #syntax error here
                AND (street_name2 IS NOT NULL)
                AND (id > %s));''', (rowdate, rowid))

Running psql 9.3.14, python 2.7

You cannot put a WHERE clause into an INSERT statement - insert inserts new rows. You are probably looking for the UPDATE statement.

I can only guess, but you might want something like this:

UPDATE tablename 
    SET got_date = (%s)
    WHERE ((suburb IS NOT NULL)
        AND (street_name2 IS NOT NULL)
        AND (id > %s));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM