Error ------ unexpected EOF while parsing

时间:2019-01-09 22:23:21

标签: python mysql-python eof

I am running this code on Python 3(IDLE) on my raspberry pi 3 whith the latest raspbian software. With this code I am trying to obtain temperature data through ds18b20 sensor and sending that same data towards the mysql database I created.

From the try: to the end of the if connection.is_connected(), I am establishing the connection to the mysql database.

From the if os.system('modprobe w1-gpio') to return temp_c, I am obtaining the temperature data through the ds18b20 sensor.

From the Whiletrue to the end of my code, I try to send the temperature data into a specific table intitled TAB_CLASSROOM.

Help would be very much appreciated!

HERE IS THE FULL ERROR!:

Traceback (most recent call last):
  File "/home/pi/Desktop/mysqlfinal1test.py", line 74
    db.close()
              ^
SyntaxError: unexpected EOF while parsing

Here is the python including mysql code :

import os
import glob
import time
import MySQLdb
import datetime
import mysql.connector
from mysql.connector import Error

i = datetime.datetime.now()

try:
    connection = mysql.connector.connect(host='127.0.0.1',
                             database='temp_pi',
                             user='root',
                             password='test')

    if connection.is_connected():
       db_Info = connection.get_server_info()
       print("Connected to MySQL database... MySQL Server version on ",db_Info)
       cursor = connection.cursor()
       cursor.execute("select database();")
       record = cursor.fetchone()
       print ("Your connected to - ", record)

    os.system('modprobe w1-gpio')
    os.system('modprobe w1-therm')

    base_dir = '/sys/bus/w1/devices/'
    device_folder = glob.glob(base_dir + '28*')[0]
    device_file = device_folder + '/w1_slave'

    def read_temp_raw():
        f = open(device_file, 'r')
        lines = f.readlines()
        f.close()
        return lines

    def read_temp():
        lines = read_temp_raw()
        while lines[0].strip()[-3:] != 'YES':
            time.sleep(0.2)
            lines = read_temp_raw()
        equals_pos = lines[1].find('t=')
        if equals_pos != -1:
            temp_string = lines[1][equals_pos+2:]
            temp_c = float(temp_string) / 1000.0
            temp_f = temp_c * 9.0 / 5.0 + 32.0
            return temp_c  

        while True:
              print("recording data into database(period = 5s.)....press ctrl+Z to stop!")

              valT = str(read_temp())

              year = str(i.year)
              month = str(i.month)
              day = str(i.day)
              date = day + "-" + month + "-" + year

              hour = str(i.hour)
              minute = str(i.minute)
              second = str(i.second)
              timestr = hour + ":" + minute + ":" + second

              try:
                 cur.execute("""INSERT INTO TAB_CLASSROOM(temp_c,T_Date,T_Time) VALUES(%s,%s,%s)""",(valT,date,time))
                 db.commit()
              except:
                 db.rollback()

              time.sleep(10)

        cur.close()  
        db.close()

1 个答案:

答案 0 :(得分:1)

Most of posted program is a huge try block with no except clause. Thus, when the parser hits the bottom of the file, it has no way to finish off the open control block.

The try is at line 11; we run out of input after line 74, without any except for that try.

I suspect that your indentation is faulty (among other things), since this try includes two function definitions. Still, you have two try statements, and only the one except.