Re: INET type and arrays: too much casting?

Stephane Bortzmeyer <[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Organization NIC France
Message-ID <[email protected]>
On Mon, Sep 15, 2008 at 10:25:12PM +0200,
 Stephane Bortzmeyer <[email protected]> wrote 
 a message of 45 lines which said:

> For my IP addresses, should I use adapters?

Something like:

import psycopg2
from psycopg2.extensions import adapt, register_adapter, AsIs

class Inet(object):
    def __init__(self, list):
        # TODO: check they are IP addresses or we risk SQL injection
        self.list = list

def adapt_inet(obj):
    return AsIs("'{%s}'" % ",".join (obj.list))

register_adapter(Inet, adapt_inet)

conn = psycopg2.connect("dbname=essais")
curs = conn.cursor()
# CREATE TABLE mytable (addrs INET[]);
curs.execute("INSERT INTO mytable (addrs) VALUES (%s)",
             (Inet(["2001:db8:::1", "176.34.0.2",
	     "2001:dead:babe:1:a:b:cafe:1"]), ))

Am I on the right track?
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.