Chcę przejść z bazy Sqlite3 na MyQSL ale mam problem z Pythonem i dodawaniem do bazy Myqsl z przykładu który podał Tytan chodzi o to że wywala mi błąd w konsoli ale do bazy dodaje i kasuje proces a błąd z którym nie mogę sobie poradzić wygląda tak:
Traceback (most recent call last):
File "/mnt/dysk/meteo/meteo22.py", line 77, in <module>
c.execute("""INSERT INTO meteo (data, CA, TC, W1, W2, TW1, TW2, T0, T1, T2, T3, T4, T5, T6) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", (now, CA, TC, W1, W2, TW1, TW2, T0, T1, T2, T3, T4, T5, T6))
File "/usr/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.InterfaceError: (0, '')
skrypt wygląda tak:
# cat meteo.py
import serial
import sys
import MySQLdb
import time
import datetime
conn = MySQLdb.connect(host="host", user="user", passwd="passwd", db="meteo")
f1=0
f2=0
f3=0
f4=0
f5=0
f6=0
f7=0
f8=0
f9=0
f10=0
f11=0
f12=0
f13=0
# open serial port
ser = serial.Serial('/dev/ttyACM0',115200,timeout=2, bytesize=8, parity='N', stopbits=1, xonxoff=1, rtscts=1)
while True:
line = ser.readline().strip()
if line[:2] == "CA":
CA = line[2:]
f1 = 1
elif line[:2] == "TC":
TC = line[2:]
f2 = 1
elif line[:2] == "W1":
W1 = line[2:]
f3 = 1
elif line[:2] == "W2":
W2 = line[2:]
f4 = 1
elif line[:3] == "TW1":
TW1 = line[3:]
f5 = 1
elif line[:3] == "TW2":
TW2 = line[3:]
f6 = 1
elif line[:2] == "T0":
T0 = line[2:]
f7 = 1
elif line[:2] == "T1":
T1 = line[2:]
f8 = 1
elif line[:2] == "T2":
T2 = line[2:]
f9 = 1
elif line[:2] == "T3":
T3 = line[2:]
f10 = 1
elif line[:2] == "T4":
T4 = line[2:]
f11 = 1
elif line[:2] == "T5":
T5 = line[2:]
f12 = 1
elif line[:2] == "T6":
T6 = line[2:]
f13 = 1
if (f1 == 1 and f2 == 1 and f3 == 1 and f4 == 1 and f5 == 1 and f6 == 1 and f7 == 1 and f8 == 1 and f9 == 1 and f10 == 1 and f11 == 1 and f12 == 1 and f13 == 1):
now = datetime.datetime.now()
now = now.strftime("%Y-%m-%d %H:%M:%S")
c = conn.cursor()
c.execute("""INSERT INTO meteo (data, CA, TC, W1, W2, TW1, TW2, T0, T1, T2, T3, T4, T5, T6) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", (now, CA, TC, W1, W2, TW1, TW2, T0, T1, T2, T3, T4, T5, T6))
f1 = 0
f2 = 0
f3 = 0
f4 = 0
f5 = 0
f6 = 0
f7 = 0
F8 = 0
f9 = 0
f10 = 0
f11 = 0
f12 = 0
f13 = 0
conn.commit()
conn.close()
Dodanie na końcu ponownie conn.close() naprawia częściowo błąd ale nadal wyskakuje taki
Traceback (most recent call last):
File "/mnt/dysk/meteo/meteo22.py", line 94, in <module>
conn.close()
_mysql_exceptions.ProgrammingError: closing a closed connection
Proszę o sugestie bo nie mam pomysłu jak to naprawić