python网络编程学习笔记(九):数据库客户端 DB-API

2019-10-06 14:08:48刘景俊

cur=dbh.cursor()

cur.execute("SELECT * FROM asd")

for column in cur.description:
    print column

dbh.close()

八、计算行数
方法有两种,一种是用len(),一种是用rowcount。


import psycopg2
print "connecting to bbstime"
dbh=psycopg2.connect('dbname=bbstime user=postgres')
print "connection successful"
cur=dbh.cursor()
cur.execute("SELECT * FROM test")
rows=cur.fetchall()
print len(rows)#利用len来计算行数
print "rows:",cur.rowcount#利用rowcount来计算行数
dbh.close()