2017-08-01 77 views
0

我是Python編程的初學者(不超過6個月的學習和實踐)。 我有一個GPS追蹤器靠近我的車,另一個靠近我的父親車。 我想將由這兩個GPS追蹤器發送的數據記錄到數據庫以備將來統計。 一切工作正常,直到我嘗試寫入數據通過TCP接收到MySQL表中。Python 3 MySQL GPS跟蹤器數據庫插入錯誤

我試圖找到解決我的問題,但我沒有成功(here,here,here)。 任何ideea,幫助,無論我會多麼小將會被折服。

Console error message Get data from ('tracker_ip', 25904) ('tracker_id', latitude, longitude, '102456', '010817', 'N', 'E', '0', '0.00', 10.3) error: uncaptured python exception, closing channel <main.EchoHandler connected tracker_ip:25904 at 0x7f627f6e2b38> (:dbinsert() missing 1 required positional argument: 'data_tuple' [/usr/lib/python3.5/asyncore.py|read|83] [/usr/lib/python3.5/asynco re.py|handle_read_event|423] [server3_1.py|handle_read|84])

我的代碼

#!/usr/bin/python3 
import pymysql 
import asyncore 
import socket 
import re 


class Database(): 
    def __init__(self): 
     self.dbconnection = pymysql.connect('localhost','db_user','password','db') 
     self.cursor = self.dbconnection.cursor() 

     sql = """CREATE TABLE IF NOT EXISTS GPS (
      signature CHAR(50) NOT NULL, 
      latitude REAL, 
      orientationNS CHAR(1), 
      longitude REAL, 
      orientationEV CHAR(1), 
      gpstime CHAR(10), 
      gpsdate CHAR(10), 
      speed CHAR(6), 
      direction CHAR(3), 
      distance REAL)""" 
     self.cursor.execute(sql) 
     self.dbconnection.commit() 


    def dbinsert(self,data_tuple): 

     cmd =("""INSERT INTO GPS (signature,latitude,longitude,gpstime,gpsdata,orientationNS,orientationEV,direction,speed,distance)\ 
      VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""")    
     self.cursor.execute(cmd,data_tuple) 
     self.dbconnection.commit() 



    def __del__(self): 
     self.dbconnection.close() 



class ClientHandler(asyncore.dispatcher): 

    def ConvertCoordinates(self,latitude,longitude): 
     lat_degree = int(float(latitude)/100); 
     lng_degree = int(float(longitude)/100); 
     # Extracting minutes for latitude and longitude 
     lat_mm_mmmm = float(latitude) % 100 
     lng_mm_mmmmm = float(longitude) % 100 

     converted_latitude = lat_degree + (lat_mm_mmmm/60) 
     converted_longitude = lng_degree + (lng_mm_mmmmm/60) 
     return (converted_latitude,converted_longitude) #tuple 

    def GetData(self,data_string): 
     data_list = re.sub(r'\s', '', data_string).split(',') 
     signature = data_list[1] 
     latitude,longitude = self.ConvertCoordinates(data_list[5],data_list[7]) 
     orientationNS = data_list[6] #latitude 
     orientationEV = data_list[8] #longitude 
     gpstime = data_list[3] 
     gpsdate =data_list[11] 
     gps_fix_validate = data_list[4] # 
     speed= data_list[9] 
     direction = data_list[10] 
     distance = 10.3 #dummy data 
     return (signature,latitude,longitude,gpstime,gpsdate,orientationNS,orientationEV,direction,speed,distance) 


    def handle_read(self): 
     data = self.recv(8192) 
     data_string = data.decode(errors="ignore") 
     #print(len(data_string)) 
     if len(data_string) >= 84 and len(data_string) <= 88 : 
      data_tuple = self.GetData(data_string) 
      print(data_tuple) 
      Database.dbinsert(data_tuple) 


class DataServer(asyncore.dispatcher): 

    def __init__(self, host, port): 
     asyncore.dispatcher.__init__(self) 
     self.create_socket(socket.AF_INET, socket.SOCK_STREAM) 
     self.set_reuse_addr() 
     self.bind((host, port)) 
     self.listen(5) 

    def handle_accept(self): 
     pair = self.accept() 
     if pair is None: 
      return 
     else: 
      sock, addr = pair 
      print('Get data from %s' % repr(addr)) 
      handler = ClientHandler(sock) 

database=Database() 
server = DataServer('192.168.10.10', 21112) 
asyncore.loop() 

回答

0

我喜歡你的汽車與GPS的使用做什麼。是不是那麼容易調試這種事情沒有一個前面的工作代碼與變量玩,但有一些想法,已經使用pymysql了很多,也得到了錯誤missing 1 required positional argument

事情嘗試:

  1. cmd =("""INSERT INTO GPS (signature,latitude,longitude,gpstime,gpsdata,orientationNS,orientationEV,direction,speed,distance) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""")左右丟掉括號,並將其放在一行中,即使這違反了PEP 8規則。已經檢查過我的代碼,並且這個字符串沒有括號。

  2. 嘗試一組額外的括號這裏就像這樣:self.cursor.execute((cmd,data_tuple))

  3. 嘗試一組額外的括號內的handle_read功能: Database.dbinsert((data_tuple))

  4. 添加cursorclass=pymysql.cursors.DictCursor的事物在方括號在這裏('localhost','db_user','password','db')你可能甚至想用手柄填充這些東西,host='localhost'等是安全的。

無論如何,我希望這些東西的作品,剛剛從我的經驗,錯誤信息出現時,有什麼不對的,因爲這些變量在你的函數之間傳遞的括號內。