本文整理汇总了Python中psycopg2.extras.RealDictCursor方法的典型用法代码示例。如果您正苦于以下问题:Python extras.RealDictCursor方法的具体用法?Python extras.RealDictCursor怎么用?Python extras.RealDictCursor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psycopg2.extras
的用法示例。
在下文中一共展示了extras.RealDictCursor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: database_connection
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import RealDictCursor [as 别名]
def database_connection(database_url):
parsed = urlparse(database_url)
user = parsed.username
password = parsed.password
host = parsed.hostname
port = parsed.port
database = parsed.path.strip('/')
try:
connection = psycopg2.connect(
host=host,
port=port,
user=user,
password=password,
database=database,
cursor_factory=RealDictCursor)
except psycopg2.OperationalError:
raise UnableToConnectToDatabase
connection.set_session(autocommit=True)
return connection
示例2: execute_cursor
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import RealDictCursor [as 别名]
def execute_cursor(self, sql, arguments):
with self._get_connection() as conn:
cursor = conn.cursor(cursor_factory=RealDictCursor)
try:
cursor.execute(sql, arguments)
conn.commit()
return cursor
except:
if not conn.closed:
conn.rollback()
# cursor.close()
# retry execution
conn = self.connectionPool.getconn()
try:
cursor = conn.cursor(cursor_factory=RealDictCursor)
cursor.execute(sql, arguments)
conn.commit()
except Exception as e:
if not conn.closed:
conn.rollback()
print(e)
示例3: insert
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import RealDictCursor [as 别名]
def insert(self, sql, values):
with self._get_connection() as conn:
cursor = conn.cursor()
try:
execute_values(cursor, sql, values)
conn.commit()
except:
if not conn.closed:
conn.rollback()
# cursor.close()
# retry execution
conn = self.connectionPool.getconn()
try:
cursor = conn.cursor(cursor_factory=RealDictCursor)
execute_values(cursor, sql, values)
conn.commit()
except Exception as e:
if not conn.closed:
conn.rollback()
print(e)
示例4: get_personal_timetable_rows
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import RealDictCursor [as 别名]
def get_personal_timetable_rows(upi):
set_id = settings.ROOMBOOKINGS_SETID
# Get from Django's ORM to raw psycopg2 so that a new cursor
# factory can be used to fetch dicts.
wrapped_connection = connections['gencache']
if wrapped_connection.connection is None:
cursor = wrapped_connection.cursor()
raw_connection = wrapped_connection.connection
bucket = 'a' if Lock.objects.all()[0].a else 'b'
with raw_connection.cursor(cursor_factory=RealDictCursor) as cursor:
cursor.callproc(
'get_student_timetable_' + bucket,
[
upi,
set_id
]
)
rows = cursor.fetchall()
return rows
示例5: execute_query
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import RealDictCursor [as 别名]
def execute_query(conn, query):
try:
cursor = conn.cursor(cursor_factory=RealDictCursor)
cursor.execute(query)
rows = list(cursor.fetchall())
return rows
except Exception:
# TODO this kind of thing is only necessary if we re-use connections
# If any exception occurs during query execution, close the
# connection to ensure it is not in an invalid state. The
# connection pool knows to create new connections to replace
# those that are closed
try:
conn.close()
except Exception:
pass
raise
示例6: execute
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import RealDictCursor [as 别名]
def execute(conn, query, query_vars=()):
"""Execute given query and return fetched results"""
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
cursor.execute(query, query_vars)
return cursor.fetchall()
示例7: execute
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import RealDictCursor [as 别名]
def execute(conn, query):
"""Execute given query and return fetched results"""
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
cursor.execute(query)
return cursor.fetchall()
示例8: dictCursor
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import RealDictCursor [as 别名]
def dictCursor():
""""Pull a dictionary cursor from the connection."""
return connect().cursor(cursor_factory=pg2ext.RealDictCursor)
示例9: query_cursor
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import RealDictCursor [as 别名]
def query_cursor(self, q, lazy_fetch=False, commit=True):
"""Execute a query and yield a cursor.
All execution performed by the Postgres object uses this method.
Args:
q (str): SQL query
lazy_fetch (bool): whether to use a server-side cursor (lazily fetches results).
"""
self.cursors_opened += 1
if self.verbose:
logging.debug(q)
if self.debug:
empty_cursor = Bunch()
empty_cursor.fetchmany = lambda size: []
empty_cursor.fetchall = lambda: []
yield empty_cursor
return
cursor_name = 'server_side_{}'.format(self.cursors_opened) if lazy_fetch else None
with self.connection.cursor(cursor_name, cursor_factory=RealDictCursor) as cursor:
cursor.execute(q)
yield cursor
if commit:
self.commit()
示例10: fetch_last_known_blocks
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import RealDictCursor [as 别名]
def fetch_last_known_blocks(self, count):
"""Fetches the specified number of most recent blocks
"""
fetch = """
SELECT block_num, block_id FROM blocks
ORDER BY block_num DESC LIMIT {}
""".format(count)
with self._conn.cursor(cursor_factory=RealDictCursor) as cursor:
cursor.execute(fetch)
blocks = cursor.fetchall()
return blocks
示例11: fetch_block
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import RealDictCursor [as 别名]
def fetch_block(self, block_num):
fetch = """
SELECT block_num, block_id FROM blocks WHERE block_num = {}
""".format(block_num)
with self._conn.cursor(cursor_factory=RealDictCursor) as cursor:
cursor.execute(fetch)
block = cursor.fetchone()
return block
示例12: fetch_agent_resource
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import RealDictCursor [as 别名]
def fetch_agent_resource(self, public_key):
fetch = """
SELECT public_key, name, timestamp FROM agents
WHERE public_key='{0}'
AND ({1}) >= start_block_num
AND ({1}) < end_block_num;
""".format(public_key, LATEST_BLOCK_NUM)
async with self._conn.cursor(cursor_factory=RealDictCursor) as cursor:
await cursor.execute(fetch)
return await cursor.fetchone()
示例13: fetch_all_agent_resources
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import RealDictCursor [as 别名]
def fetch_all_agent_resources(self):
fetch = """
SELECT public_key, name, timestamp FROM agents
WHERE ({0}) >= start_block_num
AND ({0}) < end_block_num;
""".format(LATEST_BLOCK_NUM)
async with self._conn.cursor(cursor_factory=RealDictCursor) as cursor:
await cursor.execute(fetch)
return await cursor.fetchall()
示例14: fetch_auth_resource
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import RealDictCursor [as 别名]
def fetch_auth_resource(self, public_key):
fetch = """
SELECT * FROM auth WHERE public_key='{}'
""".format(public_key)
async with self._conn.cursor(cursor_factory=RealDictCursor) as cursor:
await cursor.execute(fetch)
return await cursor.fetchone()
示例15: fetch_all_record_resources
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import RealDictCursor [as 别名]
def fetch_all_record_resources(self):
fetch_records = """
SELECT record_id FROM records
WHERE ({0}) >= start_block_num
AND ({0}) < end_block_num;
""".format(LATEST_BLOCK_NUM)
async with self._conn.cursor(cursor_factory=RealDictCursor) as cursor:
try:
await cursor.execute(fetch_records)
records = await cursor.fetchall()
for record in records:
fetch_record_locations = """
SELECT latitude, longitude, timestamp
FROM record_locations
WHERE record_id='{0}'
AND ({1}) >= start_block_num
AND ({1}) < end_block_num;
""".format(record['record_id'], LATEST_BLOCK_NUM)
fetch_record_owners = """
SELECT agent_id, timestamp
FROM record_owners
WHERE record_id='{0}'
AND ({1}) >= start_block_num
AND ({1}) < end_block_num;
""".format(record['record_id'], LATEST_BLOCK_NUM)
await cursor.execute(fetch_record_locations)
record['locations'] = await cursor.fetchall()
await cursor.execute(fetch_record_owners)
record['owners'] = await cursor.fetchall()
return records
except TypeError:
return []