我正在尝试为到达办公室的客人创建客人登记表。我无法将输入从表单发送到SQLite3数据库。
如果有人可以帮助我,那会很棒。
提前谢谢您
HTML:{% block body %}
Gasten registratie
Vul dit formulier in om te registreren
Voornaam
Achternaam
Bedrijfsnaam
Datum
In het kader van onze ISAE certificering registreren wij uw gegevens.
Wij verwerken uw gegevens volgends de regels van het AVG.
Registreren
{% endblock %}
还有我的python脚本:from flask import Flask, render_template, url_for, request, redirect
from datetime import datetime
import sqlite3
from sqlite3 import Error
app = Flask(__name__)
# Here I create a DB and a connection
def create_connection(db_file):
connection = None;
try:
conn = sqlite3.connect(db_file)
print(sqlite3.version)
except Error as e:
print(e)
return conn
# This is were I call the function for my db
create_connection("gast.db")
# This is were I create the tables I want in the database
def create_table(conn, guests):
sql = """ INSERT INTO guests(voor_naam,achter_naam,bedrijfs_naam,datum)
VALUES(?,?,?,?) """
c = conn.cursor()
c.execute(sql,guests)
return cur.lastrowid
# here I insert the created tables
def main():
database = r"gast.db"
sql_create_guests_table = """ CREATE TABLE IF NOT EXISTS guests (
id integer PRIMARY KEY,
voor_naam text NOT NULL,
achter_naam text NOT NULL,
bedrijfs_naam text NOT NULL,
datum date NOT NULL
); """
conn = create_connection(database)
if conn is not None:
# maak gast table
create_table(conn, sql_create_guests_table)
else:
print("Error! cannot create the database connection.")
# index for the html page
@app.route('/')
def index():
return render_template("index.html")
# sending input form form to db
@app.route('/my_form', methods=['POST'])
def my_form():
if request.method == 'POST':
c = conn.cursor()
guest_vnaam = request.form.get('Voornaam')
guest_anaam = request.form.get('Achternaam')
guest_cnaam = request.form.get('Bedrijfsnaam')
guest_datum = request.form.get('Datum')
try:
sql = ("INSERT INTO databasename.tablename (columnName,columnName,columnName,columnName Ci) VALUES (%s, %s, %s, %s)")
c.execute(sql,(guest_vnaam, guest_anaam, guest_cnaam, guest_datum))
connection.commit()
#or "conn.commit()" (one of the two)
return redirect('/')
except:
return 'Er ging iets fout met het opslaan van uw gegevens'
# This is where I run the app
if __name__ == '__main__':
app.run(debug=True)
该应用程序运行,但是当我单击注册按钮时,出现以下错误:
文件“ /home/ronny/Desktop/registratieinc/app.py”,第70行,在my_form中c = conn.cursor()NameError:未定义名称'conn'
再次欢迎您提供任何帮助或建议(;
再次感谢!