0

So I was trying to use this exception handling in python. I'm using python2.7 and flask for this. Also I am new to both python and flask so I must be doing something wrong here.

if test:
    cursor = conn.cursor()
    try:
        print cursor.execute("INSERT INTO Users (email, password, firstname, lastname, home, gender, dob, bio, profile_Image) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}')".format(email, password, firstname, lastname, home, gender, dob, bio, photo_data))
        conn.commit()
        #log user in
        user = User()
        user.id = email
        flask_login.login_user(user)
        uid = getUserIdFromEmail(flask_login.current_user.id)
        today = str(date.today())
        print today
    except Exception as e:
        print e
        print "Something Went wrong"
        return flask.redirect(flask.url_for('register'))
    print cursor.execute("INSERT INTO Album (uid, aname, adate, cover) VALUES ('{0}', 'default', '{1}', '{2}')".format(uid, today, photo_data))
    aid = getAIDfromAname('default', uid)
    cursor.execute("INSERT INTO Photo (uid, aid, data, caption) VALUES ('{0}', '{1}', '{2}', 'profile')".format(uid,aid,photo_data))
    cursor.execute("INSERT INTO Scoreboard (uid) VALUES ('{0}')".format(uid))
    conn.commit()
    finally:
        cursor.close()
    return render_template('profile.html', firstname=firstname, message='Account Created!')

else:
    print "couldn't find all tokens"
    return render_template('register.html', message='Email Already Exists')

and then It gives me this error if I run the app

  File "app.py", line 540
finally:
      ^SyntaxError: invalid syntax

I am wondering why it's giving me the error :/

1
  • 3
    Your indentation is off. The try/exept block is ended the outdented print below it. Commented Nov 10, 2018 at 18:53

1 Answer 1

3
if test:
    cursor = conn.cursor()
    try:
        print cursor.execute("INSERT INTO Users (email, password, firstname, 
lastname, home, gender, dob, bio, profile_Image) VALUES ('{0}', '{1}', '{2}', 
'{3}', '{4}', '{5}', '{6}', '{7}', '{8}')".format(email, password, firstname, 
lastname, home, gender, dob, bio, photo_data))
        conn.commit()
        #log user in
        user = User()
        user.id = email
        flask_login.login_user(user)
        uid = getUserIdFromEmail(flask_login.current_user.id)
        today = str(date.today())
        print today
    except Exception as e:
        print e
        print "Something Went wrong"
        print cursor.execute("INSERT INTO Album (uid, aname, adate, cover) VALUES ('{0}', 'default', '{1}', '{2}')".format(uid, today, photo_data))
        aid = getAIDfromAname('default', uid)
        cursor.execute("INSERT INTO Photo (uid, aid, data, caption) VALUES ('{0}', '{1}', '{2}', 'profile')".format(uid,aid,photo_data))
        cursor.execute("INSERT INTO Scoreboard (uid) VALUES ('{0}')".format(uid))
        conn.commit()
        return flask.redirect(flask.url_for('register'))
    finally:
        cursor.close()
    return render_template('profile.html', firstname=firstname, message='Account 
Created!'))

else:
    print "couldn't find all tokens"
    return render_template('register.html', message='Email Already Exists'

This would work. The indentation on the line "print cursor.execute" inside except block was incorrect.

Sign up to request clarification or add additional context in comments.

2 Comments

the code after the return statement in the expectation block will never execute.
Thanks. Rookie mistake. Edited the code. Sorry, I must have looked through the entire code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.