#!/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

Because when you read lines from a file, a string is returned. Your data[3] is "0833.6323" (as a string). You can just convert it to a float first before the calculation like this: latitude=str(float(data[3])/100.0) EDIT:
I noticed you signed your post "sraven". You already have an account here (under that name) so why did you start this new one?

hi sir,
Thanks for your reply...

linedata = {'time':data[1], 'date':data[9],'latitude':latitude,'longitude':longitude,'speed':data[7]}

        try:
            #Execute the SQL command
                        cursor.execute("""INSERT INTO gps VALUES (%(id)d, %(time)s, %(date)s, %(latitude)s,%(longitude)s,%(speed)s)""", linedata)

Now tha script not inserting the data to mysql,
its giving below error:

Traceback (most recent call last):
  File "./2read.py", line 29, in <module>
    cursor.execute("""INSERT INTO gps VALUES (%(id)d,%(time)s, %(date)s, %(latitude)s,%(longitude)s,%(speed)s)""", linedata)
  File "/var/lib/python-support/python2.5/MySQLdb/cursors.py", line 151, in execute
    query = query % db.literal(args)
KeyError: 'id'

Id is a column with autoincrement , Not null and it is primary key...
I came to know error is at id, incursor.execute...if i want to insert column woih auto increment...what i have to do.

I l registered with my another email id...but i didnt get any activation key...si i couldnt activate it...today i changed my email id and activated...

thanks,
sravan

Edited 3 Years Ago by mike_2000_17: Fixed formatting

In case you didn't notice, your linedata dictionary has no key "id". Hence you receiving the "KeyError" problem. You need the ID of the column you're inserting this into if I'm correct (I barely touched on MySQL about a year ago). Is the table you're inserting this into blank, and you're building it up? Or does it contain data and you're just overwriting/editing rows?
If you're building up a blank table, why not just track the number of rows you've added to the table so far and then you can use that as the id? Set "rowsadded" to zero at the beginning of your script and then increment it after each row input to the table. That way you can have your linedata dictionary as this:

linedata = {'id':rowsadded, 'time':data[1], 'date':data[9],'latitude':latitude,'longitude':longitude,'speed':data[7]}
Comments
thanks for taking the tough ones

Hi Sir,
My table structure is below,
Now it is empty,
mysql> describe gps;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| time | varchar(30) | NO | | NULL | |
| date | varchar(30) | NO | | NULL | |
| latitude | varchar(30) | NO | | NULL | |
| longitude | varchar(30) | NO | | NULL | |
| speed | varchar(30) | NO | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
6 rows in set (0.42 sec)
I dont need to give id from this python..script.
As your idea,
i have to give rowsadded=0 and then increment for every row...but here the task is not that...i just want to insert that id automatically for every row.

thanks,
sravan

This question has already been answered. Start a new discussion instead.