本文整理匯總了Python中psycopg2.sql方法的典型用法代碼示例。如果您正苦於以下問題:Python psycopg2.sql方法的具體用法?Python psycopg2.sql怎麽用?Python psycopg2.sql使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類psycopg2
的用法示例。
在下文中一共展示了psycopg2.sql方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_execute_dsn_kwargs
# 需要導入模塊: import psycopg2 [as 別名]
# 或者: from psycopg2 import sql [as 別名]
def test_execute_dsn_kwargs():
q = 'SELECT 1'
with testing.postgresql.Postgresql() as postgresql:
url = postgresql.url()
dsn = postgresql.dsn()
conn = psycopg2.connect(dbname=dsn['database'],
user=dsn['user'],
password='',
host=dsn['host'],
port=dsn['port'])
cur = conn.cursor()
cur.execute(q)
subsegment = xray_recorder.current_segment().subsegments[0]
assert subsegment.name == 'execute'
sql = subsegment.sql
assert sql['database_type'] == 'PostgreSQL'
assert sql['user'] == dsn['user']
assert sql['url'] == url
assert sql['database_version']
示例2: test_execute_dsn_kwargs_alt_dbname
# 需要導入模塊: import psycopg2 [as 別名]
# 或者: from psycopg2 import sql [as 別名]
def test_execute_dsn_kwargs_alt_dbname():
"""
Psycopg supports database to be passed as `database` or `dbname`
"""
q = 'SELECT 1'
with testing.postgresql.Postgresql() as postgresql:
url = postgresql.url()
dsn = postgresql.dsn()
conn = psycopg2.connect(database=dsn['database'],
user=dsn['user'],
password='',
host=dsn['host'],
port=dsn['port'])
cur = conn.cursor()
cur.execute(q)
subsegment = xray_recorder.current_segment().subsegments[0]
assert subsegment.name == 'execute'
sql = subsegment.sql
assert sql['database_type'] == 'PostgreSQL'
assert sql['user'] == dsn['user']
assert sql['url'] == url
assert sql['database_version']
示例3: test_execute_dsn_string
# 需要導入模塊: import psycopg2 [as 別名]
# 或者: from psycopg2 import sql [as 別名]
def test_execute_dsn_string():
q = 'SELECT 1'
with testing.postgresql.Postgresql() as postgresql:
url = postgresql.url()
dsn = postgresql.dsn()
conn = psycopg2.connect('dbname=' + dsn['database'] +
' password=mypassword' +
' host=' + dsn['host'] +
' port=' + str(dsn['port']) +
' user=' + dsn['user'])
cur = conn.cursor()
cur.execute(q)
subsegment = xray_recorder.current_segment().subsegments[0]
assert subsegment.name == 'execute'
sql = subsegment.sql
assert sql['database_type'] == 'PostgreSQL'
assert sql['user'] == dsn['user']
assert sql['url'] == url
assert sql['database_version']
示例4: test_execute_in_pool
# 需要導入模塊: import psycopg2 [as 別名]
# 或者: from psycopg2 import sql [as 別名]
def test_execute_in_pool():
q = 'SELECT 1'
with testing.postgresql.Postgresql() as postgresql:
url = postgresql.url()
dsn = postgresql.dsn()
pool = psycopg2.pool.SimpleConnectionPool(1, 1,
dbname=dsn['database'],
user=dsn['user'],
password='',
host=dsn['host'],
port=dsn['port'])
cur = pool.getconn(key=dsn['user']).cursor()
cur.execute(q)
subsegment = xray_recorder.current_segment().subsegments[0]
assert subsegment.name == 'execute'
sql = subsegment.sql
assert sql['database_type'] == 'PostgreSQL'
assert sql['user'] == dsn['user']
assert sql['url'] == url
assert sql['database_version']
示例5: test_execute_bad_query
# 需要導入模塊: import psycopg2 [as 別名]
# 或者: from psycopg2 import sql [as 別名]
def test_execute_bad_query():
q = 'SELECT blarg'
with testing.postgresql.Postgresql() as postgresql:
url = postgresql.url()
dsn = postgresql.dsn()
conn = psycopg2.connect(dbname=dsn['database'],
user=dsn['user'],
password='',
host=dsn['host'],
port=dsn['port'])
cur = conn.cursor()
try:
cur.execute(q)
except Exception:
pass
subsegment = xray_recorder.current_segment().subsegments[0]
assert subsegment.name == 'execute'
sql = subsegment.sql
assert sql['database_type'] == 'PostgreSQL'
assert sql['user'] == dsn['user']
assert sql['url'] == url
assert sql['database_version']
exception = subsegment.cause['exceptions'][0]
assert exception.type == 'UndefinedColumn'