未加工のクエリの出力を使用するのに苦労しています。私のコードは次のとおりです。Django Rawクエリの使用
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
私は次のエラーを取得する:
currentSelectedTeam1 = currentSelectedTeams[0].teamselectionid
AttributeError: 'long' object has no attribute 'teamselectionid'
すべてのヘルプを、アランを事前に多くのおかげでいただければ幸いです。次のように
PS
それはMySQLで私のクエリの結果を助け場合は、次のとおりです。
mysql> 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)
をフィードバックをいただき、ありがとうございます。私はこれが単純なSQLの例であることを知っていますが、私はdjangoを使って実行するよりも、より効率的なraw形式のいくつかのより高度なものを持っています。クエリでは最大2つの結果しか選択できないので、フェッチャニはこの場合には必要ありません。結果を返すための良い方法がありますか?その結果、私はジャンゴのクエリーセットと同じ方法で結果を利用できますか? –
あなたは根本的な問題を理解する必要があります。これは私が答えで指摘したものです。 'fetchmany'は' size'というオプションの引数を取るか、生のSQLに 'limit 2'を追加すると、2つの結果の最大値に制限することができます。もう一つのオプションは 'fetchall'を調べることです - もちろん、制限2句 – karthikr
を残してください。申し訳ありませんが、私はその点を理解しているか分かりません。返される結果の数に問題はありません。私は結果にアクセスする方法を理解する必要があります。私が情報にアクセスする方法に関連する基本的なものを紛失している場合は、お詫び申し上げます。 –