0

这是我试图在 aiopg 和数据中执行的查询。我不知道它为什么会失败。

该脚本工作正常,除了表中没有行。

如果我评论该drop table行,它会引发异常(表已存在)。如果我在 PgAdmin 中删除 teable,脚本会正常创建它。问题是为什么没有插入行。我尝试了有无sep=',',没有区别。

我试着打开日志级别postgresql.conf来注意,仍然没有效果。执行查询时不会记录任何内容。

cols = ['geopos', 'build_year', 'serial', 'floors_max', 'address']
buff = io.StringIO("""
geopos,series,build_year,address,floors_max
POINT (37.954441 55.725681),ааацуке544,1900,"г. Москва, г. Зеленоград, д. 1306, к. 1",321
POINT (37.657889 55.834376),Индивидуальный,2014,"г. Москва, пр-кт. Мира, д. 188 Б, к. 1",58
POINT (37.527903 55.723237),Индивидуальный,2011,"г. Москва, ул. Мосфильмовская, д. 8",53
POINT (37.511625 55.71232),индивидуальный,1959,"г. Москва, ул. Мосфильмовская, д. 33",1960
POINT (37.520671 55.79848),Индивидуальный,2006,"г. Москва, пер. Чапаевский, д. 3",57
POINT (37.258022 55.964569),,,"обл. Московская, г. Химки, мкр. Сходня, ул. Ленинградская, д. 3",54
POINT (37.427408 55.879187),,,"обл. Московская, г. Химки, ул. Панфилова, д. 15",173"""
)

dsn = 'dbname=wand_driver_dev host=localhost user=culebron password=culebron'

async with aiopg.create_pool(dsn) as pool:
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute('drop table if exists test_table')
            await cur.execute('create table test_table (geopos geometry, series text, build_year integer, address text, floors_max integer)')

            buff.seek(0)
            cur.copy_from(buff, 'test_table', sep=',', columns=cols)
            await cur.execute('select * from test_table')
            print(list(cur.fetchall()))  # prints an empty list: []
            conn.commit()

我在进行查询之前尝试添加此行:

await cur.execute("set log_min_error_statement TO 'debug1';")

还是什么也没看到。我已将所有内容设置为debug1in postgresql.conf,并且只看到了这个:

culebron@wand_driver_dev STATEMENT:  create table loaded_table (address text, serial text, geopos geometry, build_year integer, floors_max integer)

可能 copy_from 的工作方式与执行不同。但是,如果我使 create table 语句同步,它会失败:

            await cur.execute('drop table if exists test_table')
            cur.execute('create table test_table (geopos geometry, series text, build_year integer, address text, floors_max integer)')
            buff.seek(0)
            cur.copy_from(buff, 'test_table', sep=',', columns=cols)

Postgres 驱动程序引发异常:

psycopg2.ProgrammingError: relation "loaded_table" does not exist
LINE 1: select * from loaded_table

因此,它确实尝试将数据加载到表中。

我想知道它是否默默地无法读取 CSV 格式。但不知道出了什么问题。

4

1 回答 1

2

cur.copy_from不支持异步游标:

@asyncio.coroutine
def copy_from(self, file, table, sep='\t', null='\\N', size=8192,
              columns=None):
    raise psycopg2.ProgrammingError(
        "copy_from cannot be used in asynchronous mode")

要引发错误await,请在您的代码中添加:

await cur.copy_from(buff, 'test_table', sep=',', columns=cols)

如果没有await,您就不会在主循环中看到错误。

相反,请使用常规psycopg

于 2017-03-14T06:24:00.687 回答