Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming <code>connect()</code> is a function from some <a href="http://www.python.org/dev/peps/pep-0249" rel="nofollow">DB-API 2.0</a> compatible database interface (like the built-in <code>sqlite3</code>, or the most popular <code>mysql</code> interface), <code>for (data) in cursor:</code> is iterating <code>Row</code> objects, not strings.</p> <p>When you print it out, you're effectively printing <code>str(data)</code> (by passing it to a <code>%s</code> format). If you want to encode the same thing, you have to encode <code>str(data)</code>.</p> <p>However, a better way to do it is to handle the rows as rows (of one column) in the first place, instead of relying on <code>str</code> to do what you want.</p> <p>PS, if you were trying to rely on tuple unpacking to make <code>data</code> the first element of each row, you're doing it wrong:</p> <pre><code>for (data) in cursor: </code></pre> <p>… is identical to:</p> <pre><code>for data in cursor: </code></pre> <p>If you want a one-element <code>tuple</code>, you need a comma:</p> <pre><code>for data, in cursor: </code></pre> <p>(You can also add the parens if you want, but they still don't make a difference either way.)</p> <p>Specifically, iterating the cursor will call the optional <a href="http://www.python.org/dev/peps/pep-0249/#iter" rel="nofollow"><code>__iter__</code></a> method, which returns the cursor itself, then loop calling the <a href="http://www.python.org/dev/peps/pep-0249/#next" rel="nofollow"><code>next</code></a> method on it, which does the same thing as calling <code>fetchone()</code> until the result set is exhausted, and <a href="http://www.python.org/dev/peps/pep-0249/#id20" rel="nofollow"><code>fetchone</code></a> is documented to return "a single sequence", whose type isn't defined. In most implementations, that's a special row type, like <a href="http://docs.python.org/2/library/sqlite3.html#row-objects" rel="nofollow"><code>sqlite3.Row</code></a>, which can be accessed as if it were a <code>tuple</code> but has special semantics for things like printing in tabular format, allowing by-name access, etc.</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.
 

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