postgresql - Connection of Postgres with Python on Shell -
#!/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 * data") row_count = 0 row in cur: row_count += 1 #print "abc" print "row: %s %s\n" % (row_count, row) print "hello"; print "hello there working man!!!..."; if __name__ == "__main__": main()
can me rectifying error in script since not able print rows through script?
only last print executes once python firstname.py
.
i have made changes in pg_hba.conf
suggested other answers still no success!
the behavior seeing suggests have empty database. unable reproduce problem following procedure:
create database
$ createdb -u postgres firstdb
create table
$ psql -u postgres firstdb firstdb=# create table data (id int, name varchar(20));
insert data:
firstdb=# insert data (id, name) values (1, 'alice'); insert 0 1 firstdb=# insert data (id, name) values (2, 'bob'); insert 0 1 firstdb=# insert data (id, name) values (3, 'mallory'); insert 0 1
run code
$ python dbtest.py row: 1 [1, 'alice'] hello row: 2 [2, 'bob'] hello row: 3 [3, 'mallory'] hello hello there working man!!!...
it seems working designed.
wiki
Comments
Post a Comment