Почему этот код все еще добавляет пользователей в sqlite3 db, хотя len(cursor.fetchall())
больше 0?
#just for refrence, the issue is in the next block of code
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
return db
@app.route("/add_user", methods=['POST'])
def add_user():
data = request.get_json(force=True)
cursor = get_db().cursor()
response = cursor.execute("SELECT * FROM users WHERE email = ?", (data['email'],))
print('res_length', len(response.fetchall()))
res_length = len(response.fetchall())
if res_length > 0:
return jsonify({"message": "User exists"})
else: #This always executes!!! Why?????
cursor.execute("INSERT INTO users (name, email, password) VALUES(?, ?, ?)", (data['name'], data['email'], data['password']))
get_db().commit()
get_db().close()
return jsonify({
"message": "New User added",
"status": "ok",
"status_code": 200
})
Использование Flask-1.0.3 и Python-3.7.3