Frage

Ich schreibe ein Python3-Programm, das eine PostgreSQL-Datenbank verwendet, ich verwende PY-PostgreSQL: http://Python.Projects.PostGresql.org/ als Datenbank-Treiber. Eine Tabelle speichert IP-Adressen mit dem Inet-Typ.

Ich habe die folgende vorbereitete Anweisung erstellt: generasacodicetagpre.

Beim Rufhilfe (IP) erhalte ich den folgenden Fehler:

(IP ist eine Python-String) generasacodicetagpre.

Kann mir jemand helfen, das zu debuggen und herauszufinden, warum PostgreSQL die Inet-Daten nicht mag?

danke!

War es hilfreich?

Lösung

@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

Andere Tipps

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')
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top