简体   繁体   中英

Entering data into specific field using "WHERE" in "INSERT" command in SQL? (Python)

I'm currently writing a program for a parents evening system. I have two tables, a bookings table and a teacher table - set up with the following column headings: TeacherSubject | 15:30 | 15:35 | 15:40 etc... When people make a booking, they select a teacher from a drop-down menu and also a time. Therefore, I need the bookingID added into the booking table where the teacher selected = to the same teacher in the table and where time selected = time in the database.

At the moment, my code only attempts to match the teacher, but this doesn't work as I'm getting the error of: (line 5) TypeError: 'str' object is not callable

Am I doing the whole thing wrong and is this actually possible with the way I have set the table up?

def insert(parent_name, parent_email, student_name,student_form,teacher,app_time,comments):
    conn=sqlite3.connect("parentsevening.db")
    cur=conn.cursor()
    cur.execute("INSERT INTO bookings VALUES (NULL,?,?,?,?,?,?,?)",(parent_name,parent_email,student_name,student_form,teacher,app_time,comments))
    cur.execute("INSERT INTO teachers VALUES (?) WHERE teachers = (?)" (id,teacherName,))
    conn.commit()
    conn.close()

This SQL Query is invalid.

INSERT INTO teachers VALUES (?) WHERE teachers = (?)

It should be

INSERT INTO teachers (id, name) VALUES(?, ?)

Note that I'm guessing the teachers columns (id, name) WHERE on the insert isn't valid because it's used to find data (SELECT, UPDATE, DELETE)

OK, let's take out the comments and make this into an answer.

Python error

I think your error comes from WHERE teachers = (?) have you tried WHERE teachers = ? instead.

But...

bad sql syntax

Also that command as a whole doesnt make much sense, SQL syntax wise - you seem to be trying to insert where a teacher that doesn't exist (if you are inserting them) and values on an insert does not go with where and where needs a from. ie once you've solved your python error, sqlite is going to have a fit as well.

That's already covered by another answer.

But...

probably not what you should be doing

If you have an existing teacher, you only need to insert their teacherid into table bookings . You don't have to, and in fact, you can't insert into table teachers at this point, you'd get a duplicate data error.

So, rather than fixing your second query, just get rid of it entirely.

If you can get a command line or GUI SQL tool up, try running these queries by hardcoding them by hand before coding them in Python. the sqlite command should be able to do that for you.

(recommendation) don't use insert table values

Try being explicit with insert into table (<column list>) values .... The reason is that, as soon as the table changes in some way that affects column order (possibly an alter column) the values won't line up with the implied insert list. hard to debug, hard to know what was intended at time of writing. Been there, done that. And had to debug buncha folks' code who took this shortcut, it's never fun

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM