【问题标题】:trying to move database form Sqlite3 to postgresql试图将数据库从 Sqlite3 移动到 postgresql
【发布时间】:2021-11-04 10:35:53
【问题描述】:

我使用 sqlite3 编写了一个代码,现在我正在尝试重写代码,以便它可以与我创建的 GUI 一起使用,但我无法添加条目

前端代码

from tkinter import *
import datbase2



def get_selected_row(event):
    global selected_tuple
    index=list1.curselection()[0]
    selected_tuple=list1.get(index)
    e1.delete(0,END)
    e1.insert(END,selected_tuple[1])

    e2.delete(0,END)
    e2.insert(END,selected_tuple[2])

    e3.delete(0,END)
    e3.insert(END,selected_tuple[3])

    e4.delete(0,END)
    e4.insert(END,selected_tuple[4])


    

def view_command():
    list1.delete(0,END)
    for row in datbase2.view():
        list1.insert(END,row)

def search_command():
    list1.delete(0,END)
    for row in datbase2.search(title_text.get(),Author_text.get(),Year_text.get(),ISBN_text.get()):
        list1.insert(END,row)

def add_command(): 
    datbase2.insert(title_text.get(),Author_text.get(),Year_text.get(),ISBN_text.get())
    list1.delete(0,END)
    list1.insert(END,(title_text.get(),Author_text.get(),Year_text.get(),ISBN_text.get()))

def delete_command():
    datbase2.delete(selected_tuple[0])

def update_command():
    datbase2.update(selected_tuple[0],title_text.get(),Author_text.get(),Year_text.get(),ISBN_text.get())
    print()

window=Tk()

window.wm_title("A.K BookStore v1.0")

l1=Label (window,text="Title")
l1.grid(row=0,column=0)

l2=Label (window,text="Author")
l2.grid(row=0,column=2)

l3=Label (window,text="Year")
l3.grid(row=1,column=0)

l4=Label (window,text="ISBN")
l4.grid(row=1,column=2)

title_text=StringVar()
e1=Entry(window,textvariable=title_text)
e1.grid(row=0,column=1)

Author_text=StringVar()
e2=Entry(window,textvariable=Author_text)
e2.grid(row=0,column=3)

Year_text=StringVar()
e3=Entry(window,textvariable=Year_text)
e3.grid(row=1,column=1)

ISBN_text=StringVar()
e4=Entry(window,textvariable=ISBN_text)
e4.grid(row=1,column=3)

list1=Listbox(window,height=10,width=40)
list1.grid(row=2,column=0, rowspan=6,columnspan=2)

sb1=Scrollbar(window)
sb1.grid(row=2,column=2,rowspan=6)


list1.configure(yscrollcommand=sb1.set)
sb1.configure(command=list1.yview)

list1.bind('<<ListboxSelect>>',get_selected_row)

b1=Button(window,text="View all", width=12, command=view_command)
b1.grid(row=2,column=3)

b2=Button(window,text="Search Entry", width=12, command=search_command)
b2.grid(row=3,column=3)

b3=Button(window,text="Add entry", width=12, command=add_command)
b3.grid(row=4,column=3)

b4=Button(window,text="Update Selected", width=12, command=update_command)
b4.grid(row=5,column=3)

b5=Button(window,text="Delete Selected", width=12, command=delete_command)
b5.grid(row=6,column=3)

b6=Button(window,text="Close", width=12,command=window.destroy)
b6.grid(row=7,column=3)



window.mainloop()

'''

这是 postgresql 数据库的代码

'''

import psycopg2

def create_table():
    conn=psycopg2.connect("dbname='BookStore' user='postgres' password='damilare1' host='localhost' port='5432'")
    cur=conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS book(id INTEGER PRIMARY KEY, title text, author text, year integer,isbn integer)")
    conn.commit()
    conn.close()

def insert(title,author,year,isbn):
    conn=psycopg2.connect("dbname='BookStore' user='postgres' password='damilare1' host='localhost' port='5432'")
    cur=conn.cursor()
    cur.execute("INSERT INTO book VALUES (NULL,%s,%s,%s,%s)",(title,author,year,isbn))
    conn.commit()
    conn.close()


def view():
    conn=psycopg2.connect("dbname='BookStore' user='postgres' password='damilare1' host='localhost' port='5432'")
    cur=conn.cursor()
    cur.execute("SELECT * FROM book")
    rows=cur.fetchall()
    conn.close()
    return rows

def search(title="",author="",year="",isbn=""):
    conn=psycopg2.connect("dbname='BookStore' user='postgres' password='damilare1' host='localhost' port='5432'")
    cur=conn.cursor()
    cur.execute("SELECT * FROM book WHERE title=%s OR author=%s OR year=%s OR isbn=%s",(title,author,year,isbn))
    rows=cur.fetchall()
    conn.close()
    return rows


def delete(id):
    conn=psycopg2.connect("dbname='BookStore' user='postgres' password='damilare1' host='localhost' port='5432'")
    cur=conn.cursor()
    cur.execute("DELETE FROM store WHERE item=%s", (id,))
    conn.commit()
    conn.close()


def update(id,title,author,year,isbn):
    conn=psycopg2.connect("dbname='BookStore' user='postgres' password='damilare1' host='localhost' port='5432'")
    cur=conn.cursor()
    cur.execute("UPDATE book SET title=%s, author=%s,year=%s, isbn=%s WHERE id=%s", (title,author,year,isbn,id))
    conn.commit()
    conn.close() 

create_table()

insert("Algorithm and Complexity Analysis", "Dr Fatima Fika", 2021, 1234357)
'''

但它给出了这个错误

C:\Users\Marshal\Desktop\python class\GuiTkinter>python BookStoreGUI.py
 Traceback (most recent call last):
  File "C:\Users\Marshal\Desktop\python class\GuiTkinter\BookStoreGUI.py", line 2, in <module>
    import datbase2
  File "C:\Users\Marshal\Desktop\python class\GuiTkinter\datbase2.py", line 52, in <module>
    insert("Algorithm and Complexity Analysis", "Dr Fatima Fika", 2021, 1234357)
  File "C:\Users\Marshal\Desktop\python class\GuiTkinter\datbase2.py", line 13, in insert
    cur.execute("INSERT INTO book VALUES (NULL,%s,%s,%s,%s)",(title,author,year,isbn))
psycopg2.errors.NotNullViolation: null value in column "id" of relation "book" violates not-null constraint
DETAIL:  Failing row contains (null, Algorithm and Complexity Analysis, Dr Fatima Fika, 2021, 1234357).

【问题讨论】:

  • 你为什么要为你的 book 表的 id 插入一个 NULL?这没有任何意义。
  • 重新检查table book的ddl,col id是必须的.​​..如果是serial(identity)改变强制默认seq值:cur.execute("INSERT INTO book VALUES (%s, %s,%s,%s)",(标题,作者,年份,isbn))
  • 请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example

标签: python postgresql sqlite


【解决方案1】:

不要显式尝试将 NULL 作为 id 插入(因为它被指定为不可为空)。

另外,您可能希望将其设为id SERIAL PRIMARY KEY

cur.execute("INSERT INTO book VALUES (NULL,%s,%s,%s,%s)", (title, author, year, isbn))

应该是

cur.execute("INSERT INTO book VALUES (%s,%s,%s,%s)", (title, author, year, isbn))

明确地说,

cur.execute("INSERT INTO book (title, author, year, isbn) VALUES (%s,%s,%s,%s)", (title, author, year, isbn))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 2018-05-23
    • 2016-04-13
    • 2015-07-16
    • 2012-02-20
    • 2018-10-05
    相关资源
    最近更新 更多