1

I'm trying to set up a python program to edit a database from input from my arduino, and the try: command is giving me an error and I'm not sure I understand why

import serial
import MySQLdb

dbConn = MySQLdb.connect("localhost","root","Mecool100","ISEF_DB") or die ("Could not connect to database")
cursor = dbConn.cursor()

device = '/dev/ttyACM0'

try:
    print "Trying...",device
    arduino = serial.Serial(device, 250000)
except:
    print "Failed to connect on",device

try:
    data = arduino.readline() #read data
    pieces = data.split("\t")

try:
    cursor.execute("UPDATE `ISEF_DB`.`attendance` SET `present`='1' WHERE `id` = data”)
    dbConn.commit()
    cursor.close()
except MySQLdb.IntegrityError:
    print "Failed to insert data"
finally:
    cursor.close()
except:
    print "Failed to get data"

This code is returning the following error:

  File "test.py", line 21
    try:
      ^
SyntaxError: invalid syntax

Could someone please assist me in finding my error?

Thank you :)

2
  • 1
    You did not put except clause after our previous line pieces = data.split("\t") Commented Nov 24, 2015 at 2:23
  • You can't have a try without an except/else/finally. Commented Nov 24, 2015 at 2:23

3 Answers 3

2

The others who are saying that you need except blocks after your try are right...but they're missing that you already have them. Really, the problem is that this line

cursor.execute("UPDATE `ISEF_DB`.`attendance` SET `present`='1' WHERE `id` = data”)

should be this:

cursor.execute("UPDATE `ISEF_DB`.`attendance` SET `present`='1' WHERE `id` = data")

is in fact not the same character as "; it is codepoint 146 in CP1252 (sometimes incorrectly called ANSI) or codepoint 8221 in Unicode. Do chr("”") to see for yourself.

You also need to indent the third try block, so in the end, your code should look like this:

import serial
import MySQLdb

dbConn = MySQLdb.connect("localhost","root","Mecool100","ISEF_DB") or die ("Could not connect to database")
cursor = dbConn.cursor()

device = '/dev/ttyACM0'

try:
    print "Trying...",device
    arduino = serial.Serial(device, 250000)
except:
    print "Failed to connect on",device

try:
    data = arduino.readline() #read data
    pieces = data.split("\t")

    try:
        cursor.execute("UPDATE `ISEF_DB`.`attendance` SET `present`='1' WHERE `id` = data")
        dbConn.commit()
        cursor.close()
    except MySQLdb.IntegrityError:
        print "Failed to insert data"
    finally:
        cursor.close()
except:
    print "Failed to get data"
Sign up to request clarification or add additional context in comments.

2 Comments

The OP has a proper try..except, then a try with no except, then a try with its except..finally accidentally in a string, then a hanging except. So no, they don't already have them, even accounting for the smart quote issue.
@TigerhawkT3: I assumed that the third try was supposed to be indented, and when the corresponding except and finally blocks are indented too, the hanging except matches up with the second try. Take a look at the print statements and you'll see that's how it was intended.
1

You need an except and/or finally block for the second try: block. A try without an except or finally does not make sense, so the extra block is compulsory by syntax.

In your third try block, you should move the except ahead of the finally.

Additionally, you have a gnarly quote character on this line:

cursor.execute("UPDATE `ISEF_DB`.`attendance` SET `present`='1' WHERE `id` = data”)

Make sure it's a " in your code, not a .

Comments

0

every try statement must have an except clause

try:
    1/0
except ZeroDivisionError:
    print("oops")

1 Comment

On the contrary, you can do a try - finally.

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.