Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To use a dict for inserts, you are looking for a DB driver that supports the <a href="http://www.python.org/dev/peps/pep-0249/" rel="nofollow noreferrer">"named" (placeholder) paramstyle</a> (in the link, search for "paramstyle"):</p> <p>From the <a href="http://docs.python.org/library/sqlite3.html#module-sqlite3" rel="nofollow noreferrer">sqlite3 docs</a>:</p> <pre><code>import sqlite3 con = sqlite3.connect("mydb") cur = con.cursor() who = "Yeltsin" age = 72 cur.execute("select name_last, age from people where name_last=:who and age=:age", {"who": who, "age": age}) print cur.fetchone() </code></pre> <p>Unfortunately for MySQL, the two main drivers, <a href="http://mysql-python.sourceforge.net/MySQLdb.html" rel="nofollow noreferrer">MySQLdb</a> and <a href="http://packages.python.org/oursql/" rel="nofollow noreferrer">oursql</a> do not support the "named" paramstyle. MySQLdb uses the "format" paramstyle (using '%s') and oursql uses the "qmark" paramstyle (using '?'). Both these paramstyles require you to supply a <code>list</code> rather than a <code>dict</code> of args:</p> <pre><code>x={'bar':1.0,'baz':2.0} sql='INSERT INTO foo (bar,baz) VALUES (?,?)' cur.execute(sql,[x['bar'],x['baz']]) </code></pre> <p>By the way, if you need a driver to connect with MySQL, use <a href="http://packages.python.org/oursql/" rel="nofollow noreferrer">oursql</a>. MySQLdb has historically been the go-to driver for Python-MySQL interaction, but that driver uses <strong>regex</strong> to manipulate your SQL and arguments into one quoted string which is then sent to MySQL. In contrast, oursql uses prepared statements, sends the SQL and the arguments separately, <a href="http://packages.python.org/oursql/tutorial.html#parameterization" rel="nofollow noreferrer">eschewing the need for escaping data</a>. The oursql way is the right way, and avoids <a href="https://stackoverflow.com/questions/4879606/including-db-function-calls-in-python-mysqldb-executemany/4880198#4880198">certain bugs</a> that plagues MySQLdb.</p>
    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. 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