S
S
Spoon in the brain2019-07-08 11:54:59
MySQL
Spoon in the brain, 2019-07-08 11:54:59

How to iterate over data from a Flask-MySQL database?

Good afternoon, I am creating a login form on the site, I use Flask-MySQL .
How to make iteration of data from the database using this library?
Or all the same advise to use SQLAlchemy ?
I also heard about Flask-security , but I really don't want to use it.
The code:

@app.route('/test', methods = ['GET','POST'])
def test():
  if request.method == 'POST':
    user = request.form['user']
    key = request.form['pass']
    cur.execute('SELECT * FROM `user`')
    usr = cur.fetchall()
    for us in usr:
      if user and key == us:
        return redirect('/admin')
      else:
        return redirect('/404')
  return render_template('pages/test.html')

Many thanks in advance for your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pcdesign, 2019-07-08
@vessels

Or would you still recommend using SQLAlchemy?

IMHO, in your case it is not necessary yet. Write one project in bare Flask-MySQL, so that it gets settled in your head how it all works, and then you can switch to alchemy.
On the one hand, by making your authorization, you reinvent the wheel and also an unsafe bicycle.
And from a security point of view, it's better to use Flask-security.
On the other hand, you need to learn and be able to write authorization without modules.
Double-edged sword.
About your code. You SELECT * FROM `user`took all the users in the line there, and then you compare whether there is such a user or not. And there will be 100500 billion users? So what to do? Every time twist such horror.
They usually do:
cur.execute("""select id from user where login=%s   and pass=%s""", (login, passw))
If cur.fetchone().get('id'):
    return redirect(url_for('admin'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question