首页 > 解决方案 > Python sqlite3 不更新某些表而其他表得到更新

问题描述

我正在使用 Python 后端flasksqlite3. 我的大部分程序都在工作,但我有一个烧瓶函数,在单击按钮时调用并与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();
        }

标签: javascriptpythonajaxsqliteflask

解决方案


推荐阅读