2016-09-19 9 views
0

未加工のクエリの出力を使用するのに苦労しています。私のコードは次のとおりです。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) 

答えて

2

あなたの問題は、あなたがusing cursor.fetchone()、それが複数の行を返すことを期待した結果を反復処理していることです。

上位2の結果が必要な場合は、fetchmany and limit to 2 records insteadを使用します。

これは、ボンネットの下に何が起こっているかである。

あなたが1つのレコードだけを取得しているので、currentSelectedTeams[0]は実際にそれがタイプlongであり、あなたから属性にアクセスすることができないように見えるfixturematchday列を、返却されますそれ。

別のオプションは、このクエリ結果をフェッチするためにpretty powerful Django ORMを使用することです

EDIT:

あなたが本当にcursorベースの実装に固執したい場合は、この試してみてください。

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 

注意していますエッジケースのシナリオでは、1つの行のみが返されますが、この実装は失敗します。

(あなたは...など、カーソルの戻り長さをチェックする必要があります)これはDjangoのクエリセットの実装であれば、それはこのようなものになります。

qs = Fixture.objects.filter(..).values("fixturematchday", "userselection__ teamselection1or2", "userselection__teamselectionid", "userselection__userid")[:2] 
+0

をフィードバックをいただき、ありがとうございます。私はこれが単純なSQLの例であることを知っていますが、私はdjangoを使って実行するよりも、より効率的なraw形式のいくつかのより高度なものを持っています。クエリでは最大2つの結果しか選択できないので、フェッチャニはこの場合には必要ありません。結果を返すための良い方法がありますか?その結果、私はジャンゴのクエリーセットと同じ方法で結果を利用できますか? –

+0

あなたは根本的な問題を理解する必要があります。これは私が答えで指摘したものです。 'fetchmany'は' size'というオプションの引数を取るか、生のSQLに 'limit 2'を追加すると、2つの結果の最大値に制限することができます。もう一つのオプションは 'fetchall'を調べることです - もちろん、制限2句 – karthikr

+0

を残してください。申し訳ありませんが、私はその点を理解しているか分かりません。返される結果の数に問題はありません。私は結果にアクセスする方法を理解する必要があります。私が情報にアクセスする方法に関連する基本的なものを紛失している場合は、お詫び申し上げます。 –