Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With SQL, you really want to avoid just inserting your value into the query. You normally leave that to the database adapter, which has specialized knowledge about how to avoid creating dangerous SQL from your values (SQL quotation escaping, a.k.a. SQL injection attacks).</p> <p>Unfortunately, the <a href="https://github.com/pydata/pandas/blob/master/pandas/io/sql.py" rel="nofollow"><code>pandas.io.sql</code> module</a> has only half-heartedly implemented parameter support.</p> <p>Instead of using <code>frame_query</code>, just use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.from_records.html" rel="nofollow"><code>DataFrame.from_records()</code></a> directly.</p> <p>First, generate the SQL query with parameters. The format of the SQL parameters differs from database adapter to database adapter, since the <a href="http://www.python.org/dev/peps/pep-0249/" rel="nofollow">Python DB API standard</a> allows for a few variants. I'll assume you are using MySQL here, which uses <code>%s</code> for positional parameters, echoing Python's syntax:</p> <pre><code>sql = "select * from dataBase where cus IN ({0})".format(', '.join(['%s'] * len(cus2))) </code></pre> <p>That creates enough parameters for each of the values in <code>cus2</code>. Then query the database:</p> <pre><code>cur = psql.execute(sql, con, params=cus2) rows = cur.fetchall() columns = [col_desc[0] for col_desc in cur.description] cur.close() result = DataFrame.from_records(rows, columns=columns, coerce_float=True) </code></pre> <p>Since you appear to be using the <a href="http://python-sybase.sourceforge.net/" rel="nofollow"><code>Sybase</code> module</a> module for your connection, you'll have to adjust this for the (somewhat non-standard) SQL parameter syntax that library uses. It <em>only</em> accepts named parameters, which use the form <code>@name</code>:</p> <pre><code>params = dict(('@param{0}'.format(i), v) for i, v in enumerate(cus2)) sql = "select * from dataBase where cus IN ({0})".format( ', '.join(sorted(params.keys()))) cur = psql.execute(sql, con, params=params) rows = cur.fetchall() columns = [col_desc[0] for col_desc in cur.description] cur.close() result = DataFrame.from_records(rows, columns=columns, coerce_float=True) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload