Q:

Python/MySQL IN List Error: AttributeError: 'tuple' object has no attribute 'keys'

I'm trying to dynamically build an IN list of barcodes to pass to my query. I found a trick in a different StackOverflow post that lets me do this. However, my database engine keeps throwing an error. It's Python 2.7 and MySQL 5.1.
bc_list = ['850351589', '850351541']
in_list = ','.join(['%s'] * len(bc_list))

sql = """
        SELECT col1,
        col2,col3
        FROM table
        WHERE 1=1
        AND col1 IN (%s)
      """ % in_list
rs = db.session.execute(sql, tuple(bc_list), bind=db.get_engine(current_app, 'mysql_db_uri'))

A:

Thanks to @Paul Rooney for pointing out that the second argument expected a dictionary, not a tuple. I mistakenly had the MySQL python execute docs open was reading the wrong docs! Anyway, here is my pretty inelegant solution. I am pretty new to Flask & SQLAlchemy. I'm working in a legacy app and cannot use the ORM due to some poor table schema design that I have no control of. (basically constraints are not setup properly so I can't do JOINs without altering a millions+ row table) So I have to use raw sql for this query. Anyway, here is my solution.
bc_list = ['850351589', '850351541']
in_list = "" # (:bc1, :bc2, :bc3, ...)
bc_dict = {} # { bc1: bc1, bc2: bc2, ... }
for index, item in enumerate(bc_list, start=1):
    bc = ":bc" + str(index)
    in_list += "," + bc if index > 1 else bc
    bc_dict["bc" + str(index)] = item

sql = """
        SELECT
          q.barcode_no,
          l.vrt_number,
          l.lot_no,
          l.project,
          l.scientist,
          l.cmpd_weight,
          q.quantity,
          ROUND((100000*q.quantity)/l.cmpd_weight,2) AS solvate_vol
        FROM
          compound_lot l,
          sample_quantity q
        WHERE
          q.sample_id = l.sample_id AND
          q.barcode_no  IN ({})""".format(in_list)

#return sql
#return jsonify(bc_dict)

rs = db.session.execute(sql, bc_dict)

