下面是我试图在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';")还是什么都没看到。我已经在debug1中设置了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语句同步,它将失败:
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格式。但不知道是怎么回事。
发布于 2017-03-14 06:24:00
具有异步游标的不支持:
@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,您就不会在主循环中看到错误。
相反,使用常规的精神科。
https://stackoverflow.com/questions/42768327
复制相似问题