我正在使用Flask和Sqlite3.我已经编写了一个代码,可以将3个值插入到sqlite3数据库中。
from flask import Flask, render_template, g, request, redirect, url_for, flash, session
from functools import wraps
from flask_bootstrap import Bootstrap
from wtforms import Form, StringField, PasswordField, DateTimeField
from wtforms.validators import InputRequired
import os
import sqlite3
from datetime import datetime
app = Flask(__name__)
Bootstrap(app)
app.secret_key = os.urandom(24)
app.config.from_object(__name__)
D_candata = '/var/www/FlaskApp/py/static/database/CANData.db'
D_acc = '/var/www/FlaskApp/py/static/database/acc.db'
D_appo ='/var/www/FlaskApp/py/static/database/appo.db'
class LoginForm(Form):
username = StringField(validators=[InputRequired(message='Username ist noch leer')])
password = PasswordField(validators=[InputRequired(message='Password ist noch leer')])
class DateForm(Form):
bdt = DateTimeField(validators=[InputRequired(message='Bitte eingeben')], format = '%d-%m-%Y %H:%M' )
edt = DateTimeField(validators=[InputRequired(message='Bitte eingeben')], format = '%d-%m-%Y %H:%M' )
def connect_db(db):
return sqlite3.connect(db)
def loggin_required(funct):
@wraps(funct)
def wrap(*args, **kwds):
if 'logged_in' in session:
return funct(*args, **kwds)
else:
flash('log in!!')
return redirect(url_for('login'))
return wrap
@app.route('/')
@loggin_required
def index():
g.db = connect_db(D_candata)
cur = g.db.execute('SELECT * FROM CANData LIMIT 1;')
data = [dict(Timestamp=row[0], BatteryVoltage=row[1], BatteryAmperage=row[2], BatteryLoad=row[3], Range=row[4], Speed=row[5], Mileage=row[6]) for row in cur.fetchall()]
g.db.close()
return render_template ("/html/index.html", data=data)
@app.route('/reservierung/', methods = ["GET","POST"])
@loggin_required
def reservie():
form = DateForm(request.form)
b_date = ""
e_date = ""
if request.method == "POST" and form.validate() :
b_date = form.bdt.data
e_date = form.edt.data
if b_date > e_date :
flash('Bitte den Zeitraum korrigieren ')
else:
account = 'admin'
try:
con = connect_db(D_appo)
cur = con.cursor()
flash('ok1')
cur.execute("INSERT INTO appo_info (account, b_date, e_date) VALUES (?, ?, ?)",(account, b_date, e_date)) #???
flash('ok')
con.commit()
con.close()
except:
flash('500')
con.rollback()
return render_template('/html/reservie.html', form=form)
if __name__ == "__main__":
app.run(debug=True)
我认为问题始于
def reservie():
...
cur.execute("INSERT INTO appo_info (account, b_date, e_date) VALUES (?, ?, ?)",(account, b_date, e_date))
因为我使用了闪存并尝试显示出错的地方。
当我在计算机上使用chrome(服务器是覆盆子)时,它只显示(' ok1')和(' 500')。 但是当我在覆盆子上测试时,它确实显示了(' ok')。数据显示在数据库中。
在树莓派中我可以读写。但在电脑上我只能阅读
我确定这不是身份验证的问题。数据库和其他数据库已经是 777'
我不能使用调试模式,因为它不是本地的。所以有人有想法吗?
很多