문제

PostgreSQL 데이터베이스를 사용하는 Python3 프로그램을 작성하고 있습니다. http : //python.projects.postgresql.org/ 데이터베이스 드라이버로 하나의 테이블은 INET 유형을 사용하여 IP 주소를 저장합니다.

다음 준비된 문을 작성했습니다.

aID = db.prepare('SELECT id FROM a_records WHERE ip = $1 LIMIT 1')
.

AID (IP)를 호출 할 때 다음과 같은 오류가 발생합니다.

(IP는 파이썬 문자열입니다)

 File "/usr/local/lib/python3.1/dist-packages/postgresql/driver/pq3.py", line
1517, in __call__
    c = SingleXactFetch(self, parameters)
  File "/usr/local/lib/python3.1/dist-packages/postgresql/driver/pq3.py", line
837, in __init__
    Output.__init__(self, '')
  File "/usr/local/lib/python3.1/dist-packages/postgresql/driver/pq3.py", line
622, in __init__
    self._init()
  File "/usr/local/lib/python3.1/dist-packages/postgresql/driver/pq3.py", line
854, in _init
    STEP()
  File "/usr/local/lib/python3.1/dist-packages/postgresql/driver/pq3.py", line
2612, in _pq_step
    self.typio.raise_error(x.error_message, cause = getattr(x, 'exception', Non
e))
  File "/usr/local/lib/python3.1/dist-packages/postgresql/driver/pq3.py", line
507, in raise_error
    self.raise_server_error(error_message, **kw)
  File "/usr/local/lib/python3.1/dist-packages/postgresql/driver/pq3.py", line
498, in raise_server_error
    raise server_error
postgresql.exceptions.BinaryRepresentationError: invalid address family in exte
rnal "inet" value
  CODE: 22P03
  LOCATION: File 'network.c', line 218, in network_recv from SERVER
RESULT:
  type: SingleXactFetch
  parameters: ('139.78.113.1',)
STATEMENT: [prepared]
  sql_parameter_types: ['pg_catalog.inet']
  results: (id INTEGER)
  statement_id: py:0x2ff9a50
  string: SELECT id FROM a_records WHERE ip = $1 LIMIT 1
CONNECTION: [idle]
  client_address: 127.0.0.1/32
  client_port: 51563
  version:
    PostgreSQL 8.4.7 on x86_64-pc-linux-gnu, compiled by GCC gcc-4.4.real (Debi
an 4.4.5-8) 4.4.5, 64-bit
CONNECTOR: [IP4] pq://python:***@127.0.0.1:5432/vdz
  category: None
DRIVER: postgresql.driver.pq3.Driver
.

누구든지 이것을 디버그하는 데 도움이 될 수 있으며 PostgreSQL이 INET 데이터를 좋아하지 않는 이유를 알아낼 수 있습니까?

감사합니다!

도움이 되었습니까?

해결책

@mrlanrat, it's because the python library is sending your parameters as a TEXT data type but PostgreSQL is expecting an INET data type. Your example there changes the parameters so that PostgreSQL is expecting a TEXT data type, which it then casts in to an INET data type. I know that psycopg2 supports an INET data type but I'm pretty sure the current version of py-postgresql doesn't support the data type (as of 2011-05-19). Which means you have three options:

  1. py-postgresql needs to add support for the INET data type
  2. you get to pass in data of type TEXT and then cast it to INET on the server
  3. switch to psycopg2, which does support the INET data type.

http://initd.org/psycopg/docs/extras.html#inet-data-type

다른 팁

I've been having this problem all day, and I could not find any solution, so obviously I found the solution on my own a few minutes after posting here.

I need to replace $1 with inet($1::varchar), it seems that postgresql does not like it being a string when converting to an inet value, and you must typecast it as a varchar, and then turn the value into an inet variable.

So, the working line is:

aID = db.prepare('SELECT id FROM a_records WHERE ip = inet($1::varchar) LIMIT 1')

Can anyone explain why this is, and why it wont work the way I tried to at first?

Thanks!

You should explicitly cast string to inet data type.

aID = db.prepare('SELECT id FROM a_records WHERE ip = inet($1) LIMIT 1')
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top