bruhbruh
bruhbruh

Reputation: 27

Inserting into a table where two other conditions are met

In my program, the teacher user creates a class and it is added to the database. They then input the full-name of a student, where it is split and searched for in the database. If found, the name of the class is inserted into the students row. I am having issues inserting the class name into where the values for the first and lastname are the same as those inputted by the teacher.

This is the code for the creation of the table for users:

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

This is the code used to insert the classname into the table for the student. It works up till the final command where I try inserting.

studentname = (var_studname.get())
        conn = sqlite3.connect('MyComputerScience.db')
        print (studentname)
        c = conn.cursor()
        firstName, lastName = studentname.split(" ")
        c.execute ("SELECT * FROM users WHERE FName = ? AND SName= ? AND  usertype = 'Student'", (firstName, lastName,))
        results = c.fetchall()
        if len(results) == 0:
            Label(screen6, text = "Student doesn't exist!", fg="RED").place(relx=0.101, rely=0.725, height=21, width=194) 
        else:
            Label(screen6, text = ""+firstName+" Added!", fg="Green").place(relx=0.101, rely=0.725, height=21, width=194) 
            var_inserttoclass = (var_classname.get(), firstName, lastName, )
            c.execute('insert INTO users (ClassID) WHERE FName = ? AND SName= ?  VALUES (?,?,?);', var_inserttoclass)
            conn.commit()

This is the error I get:

c.execute('insert INTO users (ClassID) WHERE FName = ? AND SName= ?  VALUES (?,?,?);', var_inserttoclass)
sqlite3.OperationalError: near "WHERE": syntax error

Upvotes: 0

Views: 33

Answers (1)

forpas
forpas

Reputation: 164064

You don't need an INSERT statement but UPDATE:

UPDATE users SET ClassName = ? WHERE FName = ? AND SName= ?;

Upvotes: 1

Related Questions