2016-06-22 7 views
2

こんにちは私はPythonでデータベースに接続していくつかの情報を取得し、電子メールを送るスクリプトに取り組んでいます。私はPsycopgで行ったクエリに問題があります。Python Psycopg2 cursor.execute returnedなし

created_at = nb_daysのすべてのユーザーを取得したいと思います。私のクエリのNavicat/pgAdminでとI'haveで非常に良い仕事クエリで53個の記録:

select a.account_name, a.email, a.user_id, a.locale, a.firstname from v_accounts a where date(a.created_at) = date(current_date - interval '2 days') 

しかし、私は私のスクリプトを実行したときに、私は、クエリの結果としてNoneを持っています。これは私のスクリプトclassです:

import psycopg2 

class MyDatabase(object): 
    db_name='you' 
    user='will' 
    pw='never' 
    host='know' 
    port='it' 

    def __init__(self, db=db_name, user=user, password=pw, host=host, port=port): 
     """Connection to db - creation of the cursor""" 
     try: 
      self.baseDonn = psycopg2.connect(host=host, port=port, database=db, user=user,password=password) 
     except Exception as err: 
      print('La connexion avec la base de données à échoué : Erreur détéctée : %s' % err) 
     else: 
      self.cursor=self.baseDonn.cursor() # Création du curseur 

    def get_data_from_accounts(self,nb_days): 
     ''' Method that retrieves data from all accounts that were created nb_days before today ''' 
     sql="select a.account_name,u.email, u.user_id,u.locale, u.firstname from accounts a inner join account_users au on a.account_id=au.account_id inner join users u on au.user_id=u.user_id where date(a.created_at) = date(current_date - interval '%s days');" 
     print(sql) 
     data=(nb_days,) 
     try: 
      records = self.cursor.execute(sql,data) 
      print('cursor-execute={}'.format(records)) 
     except Exception as err: 
      print("La requete n'est pas passée, erreur={}".format(err)) 
     else: 
      return records 

これは、主要部分

from my_db import MyDatabase 
database=MyDatabase() 

# On va récupérer les données des nouveaux accounts d'y a un jours 
days_ago_2=database.get_data_from_accounts(2) 

for personne_1 in days_ago_2: 
    # data 
    #account_name=personne_1[0] 
    email=personne_1[1] 
    user_id=personne_1[2] 
    locale=personne_1[3] 
    firstname='' if personne_1[4] is None else personne_1[4] 

    language = locale.split('_')[1] 
    activation_token= database.get_activation_token(user_id) 

    subject = call_to_template2(firstname, language,activation_token)['subject'] 
    content = call_to_template2(firstname, language,activation_token)['content'] 

    print('EMAIL={} - ID={} - LANGUE={} - FIRSTNAME={}'.format(email, user_id, language, firstname)) 
    #send_data(qui_envoie, email, subject, content, token, language) 

そして、私のエラーNone object is not iterableのでラインfor personne_1 in days_ago_2:上です。そして、私はクエリの結果を見ました。get_data_from_accounts(2) = None

+1

あなたのSQL文字列の形成は私には奇妙なようです。 "print(sql)"は出力としてあなたに何を与えるのですか? また、同じ問題があった人からの回答を得たい場合は、より正確なタイトルを付ける必要があります。あなたのタイトルは広すぎます。 – Carele

答えて

7

cursor.executeオールウェイズはNoneを返します。あなたはfetchレコードセットが必要です:

try: 
    self.cursor.execute(sql,data) 
    records = self.cursor.fetchall() 
+0

ありがとうございます!申し訳ありません、そのnoobsに関する質問私は完全に忘れてしまった "フェッチ"(( –