Python sqlite3 не обновляет некоторые таблицы, в то время как другие обновляются - PullRequest
1 голос
/ 21 января 2020

Я делаю сайт с Python бэкэндом, используя flask и sqlite3. Большая часть моей программы работает, но у меня есть функция flask, которая вызывается нажатием кнопки и выполняется параллельно с запросом ajax. Он имеет 3 строки sqlite, обновляя 3 разные таблицы, но только последняя вносит изменения в таблицу. вот функция:

@app.route('/add_slot',methods=["POST","GET"])
def add_slot():
    conn = sqlite3.connect("example.db")
    sem = request.json["sem"]
    sec = request.json["sec"]
    sub = request.json["sub"]
    teacher = request.json["teacher"]
    room_no = request.json["room_no"]
    time = int(request.json["time"])
    day = int(request.json["day"])

    cur = conn.execute("select assign_slot from teacher where teacher_name = '" + teacher + "'")
    slots = cur.fetchall()[0][0]
    slots = slots[:(day-1)*10 + time-1] + "1" + slots[(day-1)*10 + time:]


    cur = conn.execute("select availability from room where room_no = " + room_no)
    avail = cur.fetchall()[0][0]
    avail = avail[:(day-1)*10 + time-1] + "0" + avail[(day-1)*10 + time:]
    conn.execute("update room set availability = '" + avail + "' where room_no = " + room_no)
    conn.execute("update teacher set assign_slot = '" + slots + "' where teacher_name = '" + teacher + "'")
    conn.execute("update tt set subject_name = '" + sub + "', teacher_name = '" + teacher + "', room_no = " + room_no + " where semester = " + sem + " and section = '" + sec + "' and time_ = " + str(time) + " and day = " + str(day))
    conn.commit()
    conn.close()

    return

И код JS, вызывающий функцию:

The last sql line is working, which updates the table tt, but others don't. I tried executing the sql statements independently on the powershell, putting the same values for the variables as passed when the function is called, and they work in the powershell. So, why are they not working in the program?
function send_data(){
            $.ajax({
                type:'POST',
                url:'/add_slot' ,
                contentType: 'application/json;charset=UTF-8',
                data : {'sec':document.getElementById("sec").value,'sem':document.getElementById("sem").value,'sub':document.getElementById("sub").value,'day':document.getElementById("sel1").value,'time':document.getElementById("sel2").value,'teacher':document.getElementById("sel3").value,'room_no':document.getElementById("sel_r").value}
            });
            close_popup();
            initial_processing();
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...