Redrock Postgres 文档
主页 切换暗/亮/自动模式 切换暗/亮/自动模式 切换暗/亮/自动模式 返回首页

COPY 相关对象

主要对象 ( Copy, AsyncCopy) 提供在 COPY 操作期间交换数据的主要接口。这些对象通常通过方法Cursor.copy()AsyncCursor.copy()获得;然而,它们也可以直接创建,例如写入一个不是数据库的目的地(例如使用FileWriter)。

有关详细信息,请参阅使用 COPY TO 和 COPY FROM

主要的 Copy 对象

Copy 类

class psycopg.Copy(cursor, *, binary=None, writer=None)

管理一个COPY操作。

参数:

  • cursor (Cursor[Any]) – 执行操作的游标。
  • binary (Optional[bool]) – 如果为True,写入二进制格式。
  • writer (Optional[Writer]) – 要写入的目标对象。如果未指定,则写入连接cursor

如果游标已经执行了COPY操作,则不需要选择binary,因为操作结果也描述了格式。该参数在手动创建Copy对象且未对游标执行任何操作时很有用,例如在使用writer=FileWriter时。

该对象通常由 with Cursor.copy() 返回。

Copy.write_row()

write_row(row)

Write a record to a table after a COPY FROM operation.

The data in the tuple will be converted as configured on the cursor; see Data adaptation configuration for details.

Copy.write()

write(buffer)

Write a block of data to a table after a COPY FROM operation.

If the COPY is in binary format buffer must be bytes. In text mode it can be either bytes or str.

Copy.read()

read()

Read an unparsed row after a COPY TO operation.

Return an empty string when the data is finished.

返回类型:

Union[bytes, bytearray, memoryview]

Instead of using !read() you can iterate on the !Copy object to read its data row by row, using for row in copy: ....

Copy.rows()

rows()

Iterate on the result of a COPY TO operation record by record.

Note that the records returned will be tuples of unparsed strings or bytes, unless data types are specified using set_types().

返回类型:

Iterator[Tuple[Any, ...]]

Equivalent of iterating on read_row() until it returns !None

Copy.read_row()

read_row()

Read a parsed row of data from a table after a COPY TO operation.

Return !None when the data is finished.

Note that the records returned will be tuples of unparsed strings or bytes, unless data types are specified using set_types().

返回类型:

Optional[Tuple[Any, ...]]

Copy.set_types()

set_types(types)

Set the types expected in a COPY operation.

The types must be specified as a sequence of oid or PostgreSQL type names (e.g. int4, timestamptz[]).

This operation overcomes the lack of metadata returned by PostgreSQL when a COPY operation begins:

  • On COPY TO, !set_types() allows to specify what types the operation returns. If !set_types() is not used, the data will be returned as unparsed strings or bytes instead of Python objects.
  • On COPY FROM, !set_types() allows to choose what type the database expects. This is especially useful in binary copy, because PostgreSQL will apply no cast rule.

AsyncCopy 类

class psycopg.AsyncCopy(cursor, *, binary=None, writer=None)

Manage an asynchronous COPY operation.

The object is normally returned by async with AsyncCursor.copy(). Its methods are similar to the ones of the Copy object but offering an asyncio interface (await, async for, async with).

AsyncCopy.write_row()

async write_row(row)

AsyncCopy.write()

async write(buffer)

AsyncCopy.read()

async read()

返回类型:

Union[bytes, bytearray, memoryview]

Instead of using !read() you can iterate on the !AsyncCopy object to read its data row by row, using async for row in copy: ....

AsyncCopy.rows()

async rows()

返回类型:

AsyncIterator[Tuple[Any, ...]]

Use it as async for record in copy.rows(): …

AsyncCopy.read_row()

async read_row()

返回类型:

Optional[Tuple[Any, ...]]

Writer objects

New in version 3.1.

Copy writers are helper objects to specify where to write COPY-formatted data. By default, data is written to the database (using the LibpqWriter). It is possible to write copy-data for offline use by using a FileWriter, or to customize further writing by implementing your own Writer or AsyncWriter subclass.

Writers instances can be used passing them to the cursor ~psycopg.Cursor.copy() method or to the ~psycopg.Copy constructor, as the !writer argument.

Writer 类

class psycopg.copy.Writer

A class to write copy data somewhere.

This is an abstract base class: subclasses are required to implement their write() method.

Writer.write()

abstract write(data)

Write some data to destination.

Writer.finish()

finish(exc=None)

Called when write operations are finished.

If operations finished with an error, it will be passed to exc.

LibpqWriter 类

class psycopg.copy.LibpqWriter(cursor)

A Writer to write copy data to a Postgres database.

This is the writer used by default if none is specified.

FileWriter 类

class psycopg.copy.FileWriter(file)

A Writer to write copy data to a file-like object.

参数:

file (IO[bytes]) – the file where to write copy data. It must be open for writing in binary mode.

This writer should be used without executing a COPY operation on the database. For example, if records is a list of tuples containing data to save in COPY format to a file (e.g. for later import), it can be used as:

with open("target-file.pgcopy", "wb") as f:
    with Copy(cur, writer=FileWriter(f)) as copy:
        for record in records
            copy.write_row(record)

AsyncWriter 类

class psycopg.copy.AsyncWriter

A class to write copy data somewhere (for async connections).

This class methods have the same semantics of the ones of Writer, but offer an async interface.

AsyncWriter.write()

abstract async write(data)

AsyncWriter.finish()

async finish(exc=None)

AsyncLibpqWriter 类

class psycopg.copy.AsyncLibpqWriter(cursor)

An AsyncWriter to write copy data to a Postgres database.