psycopg2參數化查詢

#!/usr/bin/env python
#--- coding: utf8 ---


import psycopg2 as p2


conn = p2.connect(user='nsc', password='xxxxx', host='10.67.1.176', port=5432)
csor = conn.cursor()


#非參數化查詢
#csor.execute("select * from pg_tables where schemaname='internal_app_bsaata' and tablename='event_ip_real'")
#list參數化查詢
#sql = "select * from pg_tables where schemaname=%s and tablename=%s"
#csor.execute(sql, ['internal_app_bsaata', 'event_ip_real'])
#dict參數化查詢
sql = "select * from pg_tables where schemaname=%(db_name)s and tablename=%(tb_name)s"
csor.execute(sql, {'db_name':'internal_app_bsaata', 'tb_name':'event_ip_real'})


rst = csor.fetchall()
print rst
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章