Postgres与Shell上的Python的连接


#!/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          %sn" % (row_count, row)
print "hello";

print "Hello there I am working Man!!!...";
if __name__ == "__main__":
main()

有人可以帮助我纠正此脚本中的错误,因为我无法通过此脚本打印行?

只有最后一次打印执行一次我执行python firstname.py.

我已经按照其他答案的建议对pg_hba.conf进行了更改,但仍然没有成功!

您看到的行为表明您有一个空数据库。 我无法重现以下过程的问题:

  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!!!...
    

它似乎按设计工作。

最新更新