Select single item in MYSQLdb - Python
<p>I've been learning Python recently and have learned how to connect to the database and retrieve data from a database using MYSQLdb. However, all the examples show how to get multiple rows of data. I want to know how to retrieve only one row of data. </p>

<p>This is my current method. </p>

<pre><code>cur.execute("SELECT number, name FROM myTable WHERE id='" + id + "'")
results = cur.fetchall()
number = 0
name = ""
for result in results:
    number = result['number']
    name = result['name']
</code></pre>

<p>It seems redundant to do <code>for result in results:</code> since I know there is only going to be one result. </p>

<p>How can I just get one row of data without using the <code>for</code> loop?</p>

<p><code>.fetchone()</code> to the rescue:</p>

<pre><code>result = cur.fetchone()
</code></pre>

python mysql-python