Home Value Error:unsupported format character 'd' (0x64) at index %Id
Reply: 1

Value Error:unsupported format character 'd' (0x64) at index %Id

kiruthika
1#
kiruthika Published in 2017-12-07 12:29:51Z

I tried to insert integer values in my table, But i was subjected to "Value Error"

import psycopg2

def connect():
    con=psycopg2.connect("dbname='book_store' user='postgres' password='5283' host='localhost' port='5432' ")
    cur=con.cursor()
    cur.execute("CREATE TABLE if not exists books(id SERIAL PRIMARY KEY,title TEXT NOT NULL,author TEXT NOT NULL,year integer NOT NULL,isbn integer NOT NULL)")
    con.commit()
    con.close()

def insert(title,author,year,isbn):
    con=psycopg2.connect("dbname='book_store' user='postgres' password='5283' host='localhost' port='5432'")
    cur=con.cursor()
    cur.execute("INSERT INTO books(title,author,year,isbn) VALUES(%s,%s,%d,%d)",(title,author,year,isbn))
    con.commit()
    con.close()


connect()
insert("the sun","helen",1997,23456777)

Jeronimo
2#
Jeronimo Reply to 2017-12-07 12:41:12Z

From the Psycopg FAQ:

Q: I can’t pass an integer or a float parameter to my query: it says a number is required, but it is a number!

A: In your query string, you always have to use %s placeholders, even when passing a number. All Python objects are converted by Psycopg in their SQL representation, so they get passed to the query as strings. See Passing parameters to SQL queries.

So I guess you just have to replace the %d with %s.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.317312 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO