I am making a authentication web app , and trying to insert data in database and commiting it too but still doesn't work . I am using flask-sqlite,It says success but when I go my /list endpoint it is blank and even does't print's anything in console . Here's the full code
from flask import Flask, render_template, request, url_for
import sqlite3
app = Flask(__name__)
con = sqlite3.connect("details.db")
print("Database opened successfully")
con.execute("create table if not exists Users (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT UNIQUE NOT NULL, password TEXT NOT NULL)")
print("Table created successfully")
con.close()
@app.route('/signup', methods=['GET', 'POST'])
def signup():
msg = ""
if request.method == "POST":
try:
email = request.form["email"]
password = request.form["password"]
print(name, email)
with sqlite3.connect("details.db") as con:
cur = con.cursor()
cur.execute("INSERT into Users (email, password) values (%s, %s)",(email, password))
cur.commit()
msg = "User successfully Added"
except Exception as err:
con.rollback()
msg = "Sorry We Can't Signup You"
finally:
return render_template("res.html",msg = msg)
con.close()
@app.route("/signup-page")
def signupTemplate():
return render_template('index.html')
@app.route("/list")
def list_users():
con = sqlite3.connect("details.db")
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("select * from Users")
row = cur.fetchall()
email = ""
password = ""
for rows in row:
email = rows["email"]
password = rows["password"]
con.close()
return render_template("list.html", email=email,password=password)
app.run(debug=True)
change this two lines.
cur.execute(f"INSERT into Users (email, password) values ('{email}', '{password}')")
con.commit()
you can use format string in your query and connection.commit()
not cursor.