查询数据

最近更新时间: 2024-10-17 17:10:00

#coding=utf-8
#!/usr/bin/python
import psycopg2
try:
    conn = psycopg2.connect(database="postgres", user="tbase", password="", host="172.16.0.29", port="15432")
    print "连接数据库成功"    
    cur = conn.cursor()
    sql = "select * from tbase"
    cur.execute(sql)
    rows = cur.fetchall()
    for row in rows:
        print "ID = ", row[0]
        print "NICKNAME = ", row[1],"\n"
    conn.close()
except psycopg2.Error,msg:
    print "操作数据库出库 %s" %(msg.args[0])

运行

[tbase@VM_0_29_centos python]$ python select.py 
#连接数据库成功
ID =  1
NICKNAME =  tbase 
ID =  2
NICKNAME =  pgxz 
ID =  3
NICKNAME =  pg