2017-08-22 7 views
-1
#!/usr/bin/python 
import psycopg2 
import sys 
import psycopg2.extras 


def main(): 

     #from config import config 

     conn=psycopg2.connect(host="localhost",user="postgres",database="firstdb"); 

     cur=conn.cursor('cursor_unique_name', cursor_factory=psycopg2.extras.DictCursor) 
     cur.execute("select * from data") 
     row_count = 0 
     for row in cur: 
       row_count += 1 
       #print "abc" 
       print "row: %s   %s\n" % (row_count, row) 
       print "hello"; 



     print "Hello there I am working Man!!!..."; 

if __name__ == "__main__": 
     main() 

このスクリプトで行を印刷できないため、このスクリプトのエラーを修正できますか?シェル上でのPythonとPostgresの接続

最後の印刷が1回実行されます。python firstname.pyです。

私はpg_hba.confに変更を加えましたが、他の回答では示唆されていますが、まだ成功していません。

+2

自分自身ではありませんが、 'for row in cur'を' for row in cur.fetchall() 'に置き換えようとしましたか? –

+0

はい!私はそうしましたが、それもまったく働かなかった –

+0

私はそれを感謝しました! –

答えて

0

表示されている動作は、空のデータベースがあることを示しています。私は、次の手順で問題を再現することができませんでした:

  1. は、データベース

    $ createdb -U postgres firstdb 
    
  2. テーブルを作成します

    $ psql -U postgres firstdb 
    firstdb=# create table data (id int, name varchar(20)); 
    
  3. を作成し、いくつかのデータを挿入します。

    firstdb=# insert into data (id, name) values (1, 'alice'); 
    INSERT 0 1 
    firstdb=# insert into data (id, name) values (2, 'bob'); 
    INSERT 0 1 
    firstdb=# insert into data (id, name) values (3, 'mallory'); 
    INSERT 0 1 
    
  4. は設計どおりそれが動作しているようだあなたのコード

    $ python dbtest.py 
    row: 1   [1, 'alice'] 
    
    hello 
    row: 2   [2, 'bob'] 
    
    hello 
    row: 3   [3, 'mallory'] 
    
    hello 
    Hello there I am working Man!!!... 
    

を実行します。

+0

質問で述べたように私はdb名 "firstdb"とテーブル "data"を使って作業しています。 –

+0

はい、この回答に記載されているとおりです。あなたは質問がありましたか? – larsks

+0

私はそれを感謝しました! –

関連する問題