Django Raw query usage
<p>I am struggling to use the output of a raw query.  My code is as follows:</p>

<pre><code>cursor.execute("select f.fixturematchday, u.teamselection1or2, u.teamselectionid,u.user_id from straightred_fixture f, straightred_userselection u where u.user_id = 349 and f.fixtureid = u.fixtureid and f.fixturematchday=6 order by u.teamselection1or2")
currentSelectedTeams = cursor.fetchone()

if not currentSelectedTeams:
    currentSelectedTeam1 = 0
    currentSelectedTeam2 = 0
else:
    currentSelectedTeam1 = currentSelectedTeams[0].teamselectionid
    currentSelectedTeam2 = currentSelectedTeams[1].teamselectionid
</code></pre>

<p>I get the following error:</p>

<pre><code>currentSelectedTeam1 = currentSelectedTeams[0].teamselectionid
AttributeError: 'long' object has no attribute 'teamselectionid'
</code></pre>

<p>Any help would be appreciated, many thanks in advance, Alan.</p>

<p>PS</p>

<p>In case it helps the result of my query in MySQL is as follows:</p>

<pre><code>mysql&gt; select f.fixturematchday, u.teamselection1or2, u.teamselectionid,u.user_id from straightred_fixture f, straightred_userselection u where u.user_id = 349 and f.fixtureid = u.fixtureid and f.fixturematchday=6 order by u.teamselection1or2;
+-----------------+-------------------+-----------------+---------+
| fixturematchday | teamselection1or2 | teamselectionid | user_id |
+-----------------+-------------------+-----------------+---------+
|               6 |                 1 |              31 |     349 |
|               6 |                 2 |              21 |     349 |
+-----------------+-------------------+-----------------+---------+
2 rows in set (0.00 sec)
</code></pre>

<p>Your issue is that you are <a href="http://currentSelectedTeams%20=%20cursor.fetchone()" rel="nofollow">using <code>cursor.fetchone()</code></a> and iterating through the result expecting it to return multiple rows. </p>

<p>If you want the top 2 results, you might want to use <a href="https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchall.html" rel="nofollow"><code>fetchmany</code> and limit to 2 records instead</a>. </p>

<p>This is what is happening under the hood:</p>

<p>Since you are fetching only one record, <code>currentSelectedTeams[0]</code> is actually returning the <code>fixturematchday</code> column, which it looks like is of type <code>long</code> and you are unable to access the attribute from it. </p>

<p>Another option would be to use the <a href="https://docs.djangoproject.com/en/1.10/topics/db/queries/" rel="nofollow">pretty powerful <code>Django ORM</code></a> to fetch this query result</p>

<p>EDIT:</p>

<p>If you really want to stick with <code>cursor</code> based implementation, try this:</p>

<pre><code>cursor.execute("select f.fixturematchday, u.teamselection1or2, u.teamselectionid,u.user_id from straightred_fixture f, straightred_userselection u where u.user_id = 349 and f.fixtureid = u.fixtureid and f.fixturematchday=6 order by u.teamselection1or2 LIMIT 2")
#Note the LIMIT 2

currentSelectedTeams = cursor.fetchall()

if not currentSelectedTeams:
    currentSelectedTeam1 = 0
    currentSelectedTeam2 = 0
else:
    currentSelectedTeam1 = currentSelectedTeams[0].teamselectionid
    currentSelectedTeam2 = currentSelectedTeams[1].teamselectionid
</code></pre>

<p>Note that, in an edge case scenario, where only one row is returned, this implementation would fail. (You need to check for the cursor return length, etc.. )</p>

<p>If this were a django queryset implementation, it would look something like this:</p>

<pre><code>qs = Fixture.objects.filter(..).values("fixturematchday", "userselection__ teamselection1or2", "userselection__teamselectionid", "userselection__userid")[:2]
</code></pre>

python mysql django