Taarush V Taarush V - 6 months ago 17
MySQL Question

Error in Inserting values into a database

long_url = http://www.google.com , final_short_url = http://127.0.0.1:5000/b5
@app.route('/test',methods=['GET','POST'])

def shortessturl():
conn = sqlite3.connect('databas101.db')
c = conn.cursor()

c.execute("INSERT INTO stuff (longurl, shorturl) values (?, ?)",
(long_url,final_short_url))
conn.commit
c.close
conn.close

return render_template("shorturl.html", final_short_url= final_short_url)


I am trying to insert data into a database which has table to store two values. I tried the following code . the two variables are defined outside the function. I opened the .db file but they were no entries even after the code was executed. This is flask framework and sqlite3 library
The error shown on the site is

builtins.ValueError
ValueError: View function did not return a response


Edit: The code i wrote works. The reason it did not run is because in the same application i had used the following code which prevented conn.commit to be executed

conn.commit
c.close
conn.close

Answer

Try this and see what happens

long = 'http://www.google.com'
short = 'http://127.0.0.1:5000/b5'      

@app.route('/test',methods=['GET','POST'])
def shortessturl(long_url=long, short_url=short):
    conn = sqlite3.connect('databas101.db')
    c = conn.cursor()
    c.execute("INSERT INTO stuff (longurl, shorturl) values (?, ?)",
    (long_url, short_url))
    conn.commit()
    c.close()
    conn.close()

    return render_template("shorturl.html", final_short_url=short_url)