1

I'm trying to insert some string values into a postgresql db. namely:

macaddress,
timestamp (date time format)
type of device (string)
subtype (string)

example:

INSERT INTO device (address, tstamp, type, subtype) 
 VALUES ('00:00:00:00','2012-02-22 19:31:26','computer','notebook')

I'm using python psycopg2 script to dump the data and the following error is appearing:

c.executemany("INSERT INTO device VALUES (default, '%s','%s','%s','%s')", 
 (mac, date, typedev, subtype,))
TypeError: not all arguments converted during string formatting

the command I'm using to insert is:

c.executemany("INSERT INTO device VALUES (default, '%s','%s','%s','%s')", 
 (mac, date, typedev, subtype,))
mechanical_meat
171k25 gold badges238 silver badges231 bronze badges
asked Mar 8, 2012 at 18:18
0

1 Answer 1

4

There are a couple of problems:

  1. You don't need to quote the parameter placeholders (%s). The good people of psycopg2 will ensure that is done properly for you.
  2. executemany() expects a sequence of parameters.

So since you're only inserting one row you could just use execute():

c.execute("INSERT INTO device VALUES (default,%s,%s,%s,%s)", 
 (mac, date, typedev, subtype,))

When you need to use executemany() remember to pass it a sequence of parameters, e.g.:

c.executemany("INSERT INTO device VALUES (default,%s,%s,%s,%s)", 
 [(mac, date, typedev, subtype,),])
answered Mar 8, 2012 at 18:38
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.