Python/psycopg2 WHERE IN statement

For the IN operator, you want a tuple instead of list, and remove parentheses from the SQL string. # using psycopg2 data=(‘UK’,’France’) sql=”SELECT * from countries WHERE country IN %s” cur.execute(sql,(data,)) During debugging you can check that the SQL is built correctly with cur.mogrify(sql, (data,))

Can I bind an array to an IN() condition in a PDO query?

You’ll have to construct the query-string. <?php $ids = array(1, 2, 3, 7, 8, 9); $inQuery = implode(‘,’, array_fill(0, count($ids), ‘?’)); $db = new PDO(…); $stmt = $db->prepare( ‘SELECT * FROM table WHERE id IN(‘ . $inQuery . ‘)’ ); // bindvalue is 1-indexed, so $k+1 foreach ($ids as $k => $id) $stmt->bindValue(($k+1), $id); $stmt->execute(); … Read more