ai didi

python-3.x - 值错误 :unsupported format character 'd' (0x64) at index %Id

转载 作者:行者123 更新时间:2023-11-29 12:54:01 24 4
gpt4 key购买 nike

我试图在我的表中插入整数值,但我遇到了“值错误”

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)

enter image description here

最佳答案

来自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.

所以我猜你只需要将 %d 替换为 %s

关于python-3.x - 值错误 :unsupported format character 'd' (0x64) at index %Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47695194/

24 4 0
文章推荐: iOS Storyboard : Automatic transition from one scene to another after 5 seconds
文章推荐: ios - 保存未知数量的 NSArray
文章推荐: ios - 如何使用 Open talk iOS SDK 跟踪时间?
文章推荐: php - Android 无法从 php 获取正确的查询结果
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com