ziggy ziggy - 6 months ago 11
SQL Question

python sql modulo inserting

I need some help writing to a postgres sql table with python.

First column id is an

int
, second column day is a
float
. How do I properly format it? This does not work.

When I do
values('%d');'''%id)
it works but I cannot figure out how to insert
int
and
float
in the same line

cur.execute('''insert into test (id, long) values('%d %d');''' % (id, long))

Answer

Use %s and let Psycopg do the type adapting:

cur.execute('''insert into test (id, long) values (%s, %s);''', (id, long))