SQL数据库查询问题

时间:2016-08-11 10:46:18

标签: python sqlite

作为我正在进行的编码项目的一部分,我必须创建一个程序,将文件夹分为8个文件柜,每个文件柜有3个架子,每个架子按字母顺序排列(A,A,AB,BC,C) ,D,E,F,G,HIJ,L,M,M,M,MN,O,PQ,R,RS,S,S,T,UVW,WXYZ)。我遇到的问题是我正在编写一个函数,以便我可以在任何时候调用它并输入学生的详细信息并找出文件夹应放在哪里(哪个柜子?哪个架子?哪个位置?)

到目前为止,我发现数据库中的所有条目都具有相似的姓氏,然后按字母顺序排序,以找出新文件夹相对于具有相似姓氏的所有文件夹的位置。

    import sqlite3
cabinets = ["A","A","AB", "BC","C","D", "E","F","G", "HIJ","L","M", "M","M","MN", "O","PQ","R", "RS","S","S", "T","UVW","WXYZ"]
def location(forename,secondname):
    cabinet = ""
    shelf = ""
    position = ""
    if secondname != "" and forename != "":
        firstletter = (secondname[:(len(secondname) - (len(secondname)-1))]).lower()
        cursor = db.execute("SELECT * FROM Folders WHERE Surname LIKE ?", (firstletter + "%",))
        shelfinventory = []
        for row in cursor:
            shelfinventory.append((row[2]).lower())
        shelfinventory = sorted(shelfinventory)
        shelfposition = shelfinventory.index(secondname.lower())

        position = [cabinet, shelf, shelfposition]
        return position

db = sqlite3.connect("databasetest.db")
conn = db.cursor() 
db.execute('''CREATE TABLE IF NOT EXISTS Folders
             (StudentID INT PRIMARY KEY, Forename TEXT, Surname TEXT)''')

db.execute("INSERT INTO Folders VALUES (?, ?, ?);", ("1", "Ben", "Smath"))
db.execute("INSERT INTO Folders VALUES (?, ?, ?);", ("2", "John", "Smith"))
db.execute("INSERT INTO Folders VALUES (?, ?, ?);", ("3", "Debra", "Swith"))
db.execute("INSERT INTO Folders VALUES (?, ?, ?);", ("4", "Simon", "Stith"))

print(location("Debra", "Swith"))

感谢任何帮助

0 个答案:

没有答案