无法在具有外键的表中INSERT数据

时间:2018-02-14 19:10:38

标签: python sql tkinter insert

我想在表Committed Crimes中插入数据但是它调用了一个错误。我想要插入的数据已经添加到其他表中。例如,表Criminals的值为1(Criminal_ID),表Laws的值为3(Law_ID)。当我在提交的犯罪中插入这些数据时,它会调用错误。你知道为什么以及如何解决它?

enter image description here

from tkinter import *
import pypyodbc
import ctypes

#Create connection
con = pypyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;UserCommitSync=Yes;Threads=3;SafeTransactions=0;PageTimeout=5;MaxScanRows=8;MaxBufferSize=2048;FIL={MS Access};DriverId=25;DefaultDir=C:/Users/HP/Desktop/PITL;DBQ=C:/Users/HP/Desktop/PITL/PITL.mdb;')
cursor = con.cursor ()

form=Tk ()
form.title ("Add data")
form.geometry ('400x200')

a = Entry (form, width=10, font="Arial 16")
a.pack()
b = Entry (form, width=10, font="Arial 16")
b.pack()

def Add ():
    cursor.execute ("INSERT INTO Committed crimes (`Criminal_ID`, `Law_ID`) VALUES (?, ?)", (a.get(), b.get()))
    con.commit ()

Button=Button(form, text = 'PUSH ME', command = Add)
Button.pack()

form.mainloop ()
cursor.close ()
con.close ()

错误:

Exception in Tkinter callback Traceback (most recent call last):   File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)   File "C:\Users\HP\Desktop\PITL\ADD DATA.py", line 19, in Add
    cursor.execute ("INSERT INTO Committed crimes (`Criminal_ID`, `Law_ID`) VALUES (?, ?)", (a.get(), b.get()))   File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pypyodbc-1.3.4-py3.6.egg\pypyodbc.py", line 1617, in execute
    check_success(self, ret)   File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pypyodbc-1.3.4-py3.6.egg\pypyodbc.py", line 1007, in check_success
    ctrl_err(SQL_HANDLE_STMT, ODBC_obj.stmt_h, ret, ODBC_obj.ansi)   File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pypyodbc-1.3.4-py3.6.egg\pypyodbc.py", line 975, in ctrl_err
    raise ProgrammingError(state,err_text) pypyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][Драйвер ODBC Microsoft Access] Ошибка синтаксиса в инструкции INSERT INTO.')

0 个答案:

没有答案