當前位置: 首頁>>代碼示例>>Python>>正文


Python extras.RealDictCursor方法代碼示例

本文整理匯總了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 
開發者ID:vokal,項目名稱:pg-table-markdown,代碼行數:23,代碼來源:database.py

示例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) 
開發者ID:microsoft,項目名稱:aerial_wildlife_detection,代碼行數:24,代碼來源:app.py

示例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) 
開發者ID:microsoft,項目名稱:aerial_wildlife_detection,代碼行數:23,代碼來源:app.py

示例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 
開發者ID:uclapi,項目名稱:uclapi,代碼行數:26,代碼來源:personal_timetable.py

示例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 
開發者ID:tilezen,項目名稱:tilequeue,代碼行數:21,代碼來源:postgres.py

示例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() 
開發者ID:innogames,項目名稱:igcollect,代碼行數:7,代碼來源:postgres.py

示例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() 
開發者ID:innogames,項目名稱:igcollect,代碼行數:8,代碼來源:postgres_query.py

示例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) 
開發者ID:gogogoutham,項目名稱:coinmarketcap-scraper,代碼行數:5,代碼來源:pg.py

示例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() 
開發者ID:kelvinguu,項目名稱:lang2program,代碼行數:30,代碼來源:postgres.py

示例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 
開發者ID:hyperledger,項目名稱:education-sawtooth-simple-supply,代碼行數:15,代碼來源:database.py

示例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 
開發者ID:hyperledger,項目名稱:education-sawtooth-simple-supply,代碼行數:12,代碼來源:database.py

示例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() 
開發者ID:hyperledger,項目名稱:education-sawtooth-simple-supply,代碼行數:13,代碼來源:database.py

示例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() 
開發者ID:hyperledger,項目名稱:education-sawtooth-simple-supply,代碼行數:12,代碼來源:database.py

示例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() 
開發者ID:hyperledger,項目名稱:education-sawtooth-simple-supply,代碼行數:10,代碼來源:database.py

示例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 [] 
開發者ID:hyperledger,項目名稱:education-sawtooth-simple-supply,代碼行數:40,代碼來源:database.py


注:本文中的psycopg2.extras.RealDictCursor方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。