小编kir*_*ika的帖子

值错误:索引 %Id 处的格式字符“d”(0x64)不受支持

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

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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

postgresql psycopg2 python-3.x

2
推荐指数
1
解决办法
3091
查看次数

InternalError:当前事务被中止,命令被忽略,直到事务块结束(受UNIQUE约束)

我试图用我的except:语句执行...而试图反对UNIQUE约束的功能。但是以异常错误结束。.Postgresql数据库表已经包含我在其中使用的行

db.insert(“新闻”,“ AparnaKumar”,1995,234569654)

但在插入未重复的行时效果很好。

import psycopg2
class database:

    def __init__(self):

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

    def insert(self,title,author,year,isbn):
      try:
        self.cur.execute("INSERT INTO books(title,author,year,isbn) VALUES(%s,%s,%s,%s)",(title,author,year,isbn))
        self.con.commit()
      except:
          print("already exists..")

    def view(self):
        self.cur.execute("SELECT * FROM books")
        rows=self.cur.fetchall()
        print(rows)

    def search(self,title=None,author=None,year=None,isbn=None):
        self.cur.execute("SELECT * FROM books WHERE title=%s or author=%s or year=%s or isbn=%s",(title,author,year,isbn))
        row=self.cur.fetchall()
        print(row)

db=database()
db.insert("The …
Run Code Online (Sandbox Code Playgroud)

postgresql psycopg2 python-3.x

2
推荐指数
1
解决办法
3304
查看次数

标签 统计

postgresql ×2

psycopg2 ×2

python-3.x ×2