INSERT INTO无法正常工作。唯一约束失败(Python SQLite)

时间:2018-04-20 08:59:19

标签: python sqlite

我正在创建一个排序应用程序,它从表中挑选学生并将它们放在表单组中。不同群体的性别应该是平等的。它循环遍历每个表插入随机的男学生。

我使用打印功能来跟踪发生的事情。似乎循环正在起作用

print(idMale)

正在打印行。然而,在另一张表中根本没有插入。程序会中断并输出此错误:

error message

这是我的代码:

# loops through each table
for x in range(0,groupNumber):
    #Adds I to table name to go through each table
    nameGroupList.append('I')
    print(nameGroupList)
    nameGroup = ''.join(nameGroupList)

    #loop to select a student from the main table (sessionName) and insert them into another group
    #loop number is subject to the number of males that should be in 1 group
    for x in range(0, formMaleInt):
        selectMaleRow = cur.execute("SELECT * FROM " + sessionName + " WHERE Gender='M'  ORDER BY random() Limit 1")
        #-----Find ID of student selected so they can be deleted from sessionName
        idMale = selectMaleRow.fetchone()
        print(idMale)
        idSepChar = list(idMale)
        idNum = idSepChar[0]
        idNumStr = str(idNum)
        #-----Find ID of student selected so they can be deleted from sessionName

        insertMaleRow = cur.execute("INSERT INTO " + nameGroup + " SELECT * FROM " + sessionName + " WHERE Gender='M'  ORDER BY random() Limit 1")
        deleteMaleRow = cur.execute("DELETE FROM " + sessionName + " WHERE ID='" + idNumStr + "'")

这是我的sessionName表: sessionNameTable

这是我的另一张桌子 enter image description here

0 个答案:

没有答案