Próbuję użyć studenta, aby uzyskać pierwsze i nazwisko z jednej stołowej, a następnie uzyskaj wyniki dla jednej z ich prac domowych za pomocą zaprogramowania domowego, a wyniki wyświetlane w widgecie Listabox za pomocą TKINTER. Jednak zanim można go wyświetlić, najpierw trzeba mieć możliwość uzyskania danych. To, co mam do tej pory:

 def viewresults (self, sethw):

        print (self.Homework_To_Set.get())
        #resultslist = []
        conn = sqlite3.connect('MyComputerScience.db')
        c = conn.cursor()
        homeworkID = c.execute("SELECT HWID FROM HomeworkInfo WHERE HomeworkName = ?", (self.Homework_To_Set.get(), )).fetchone()
        studentsnamecheck = c.execute("SELECT StudentID FROM HomeworkSet WHERE  HWID = ?", ((homeworkID[0], ))).fetchall()
        if len(studentsnamecheck) == 0:
            self.View_Results.insert(END, "No one has completed this homework!")
        else:
            for x in studentsnamecheck:
                c.execute("SELECT FName || ' ' || SName FROM users WHERE UserID = ?", (str(x), ))
                res1 = (c.fetchall())
                for i in (studentsnamecheck):
                    result_collect = c.execute("SELECT Results FROM HomeworkSet WHERE StudentID = ? AND HWID = ?", ((str(i)), self.Homework_To_Set.get() ))

                var_insert_results = ((res1), (result_collect))
                self.View_Results.insert(END, str(var_insert_results))

A to jest prąd wyjściowy:

([], <sqlite3.Cursor object at 0x000001F40E1809D0>)
([], <sqlite3.Cursor object at 0x000001F40E1809D0>)
([], <sqlite3.Cursor object at 0x000001F40E1809D0>)
([], <sqlite3.Cursor object at 0x000001F40E1809D0>)
([], <sqlite3.Cursor object at 0x000001F40E1809D0>)
([], <sqlite3.Cursor object at 0x000001F40E1809D0>)
([], <sqlite3.Cursor object at 0x000001F40E1809D0>)

To jest schemat dla baz danych:

c.execute("""CREATE TABLE IF NOT EXISTS users (
            UserID INTEGER PRIMARY KEY AUTOINCREMENT,
            FName text,
            SName text,
            username text,
            password text,
            userType text,
            ClassName text);""")

c.execute("""CREATE TABLE IF NOT EXISTS ClassInfo (
            ClassID INTEGER PRIMARY KEY AUTOINCREMENT,
            ClassName text,
            Teacher text);""")

c.execute("""CREATE TABLE IF NOT EXISTS HomeworkInfo (
            HWID INTEGER PRIMARY KEY,
            HomeworkName text);""")


c.execute("""CREATE TABLE IF NOT EXISTS HomeworkSet (
            HWID integer,
            StudentID text,
            Results text,
            FOREIGN KEY (StudentID) REFERENCES users(UserID)
            FOREIGN KEY (HWID) REFERENCES HomeworkInfo(HWID));""")
-2
MyNameJeff 18 marzec 2020, 20:46

1 odpowiedź

Najlepsza odpowiedź

W Twoim kodzie występuje kilka problemów:

c.execute("SELECT FName || ' ' || SName FROM users WHERE UserID = ?", (str(x), ))
res1 = (c.fetchall())
for i in (studentsnamecheck):
    result_collect = c.execute("SELECT Results FROM HomeworkSet WHERE StudentID = ? AND HWID = ?",
                               ((str(i)), self.Homework_To_Set.get() ))
  • Nie UserID pola w tabeli users; Może być username
  • str(x) powinien być x[0]
  • powinien pętli przez res1 zamiast studentsnamecheck}
  • str(i) powinien być x[0]
  • self.Homework_To_Set.get() powinien być homeworkID[0]

Więc kod powinien być:

def viewresults(self, sethw):
    print(self.Homework_To_Set.get())
    conn = sqlite3.connect('MyComputerScience.db')
    c = conn.cursor()
    homeworkID = c.execute("SELECT HWID FROM HomeworkInfo WHERE HomeworkName = ?",
                           (self.Homework_To_Set.get(),)).fetchone()
    studentsnamecheck = c.execute("SELECT StudentID FROM HomeworkSet WHERE HWID = ?",
                                  (homeworkID[0],)).fetchall()
    if len(studentsnamecheck) == 0:
        self.View_Results.insert(END, "No one has completed this homework!")
    else:
        for x in studentsnamecheck:
            c.execute("SELECT FName||' '||SName FROM users WHERE username = ?", (x[0],))
            res1 = c.fetchall()
            for i in res1:
                results = c.execute("SELECT Results FROM HomeworkSet WHERE StudentID = ? AND HWID = ?",
                                    (x[0], homeworkID[0])).fetchone()
                self.View_Results.insert(END, '%-30s  %s'%(i[0], results[0]))

Jednak można połączyć oświadczenia SELECT w jednym zapytaniu:

def viewresults(self, sethw):
    conn = sqlite3.connect('MyComputerScience.db')
    c = conn.cursor()
    sql = """
        SELECT SName||' '||FName, Results
        FROM HomeworkSet, HomeworkInfo, users
        WHERE HomeworkInfo.HomeworkName = ?
          AND HomeworkInfo.HWID = HomeworkSet.HWID
          AND HomeworkSet.StudentID = users.username
    """
    c.execute(sql, (self.Homework_To_Set.get(),))
    for result in c:
        self.View_Results.insert(END, '%-30s  %s'%result)
0
acw1668 19 marzec 2020, 05:06