#!/usr/bin/env python
import MySQLdb
file=open("capgps.txt",'r')
#Open database connection
db = MySQLdb.connect("localhost","root","8868","myproject")
# prepare a cursor object using cursor() method
cursor = db.cursor()
#line is like below
#$GPRMC,111503,A,0833.6323,N,07652.7685,E,0.1866,256.540,220609,,*2A
for line in file:
data=line.split(",")
if data[0]=="$GPRMC" and data[2]=="A":
if data[4]=="N":
latitude=str((data[3])/100.0)
else:
latitude=str((-data[3])/100.0)
if data[5]=="E":
longitude=str(data[5]/100.0)
else:
longitude=str((-data[5])/100.0)
linedata = {'time':data[1], 'date':data[9],'latitude':latitude,'longitude':longitude,'speed':data[7]}
try:
#Execute the SQL command
#cursor.execute(sql)
cursor.execute("""INSERT INTO gps VALUES (%(id)d, %(time)s, %(date)s, %(latitude)s,%(longitude)s,%(speed)s)""", linedata)
# cursor.execute(query,(data[1],data[9],data[3],data[5],data[7]))
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()
# disconnect from server
db.close()
file.close()
i got below error:
sravan@gudivada:~/Desktop/pythonscripttest$ ./2read.py
Traceback (most recent call last):
File "./2read.py", line 15, in <module>
latitude=str((data[3])/100.0)
TypeError: unsupported operand type(s) for /: 'str' and 'float'
please explain why i am getting this error...
thanks,
sravan