shruthi
shruthi

Reputation: 71

insert csv file mysql with date time error

I am trying to insert csv file into mysql but I have error in my datetime field. I tried this but recive this error on this line..... date,time = startdatetime.split('/') ValueError: need more than 1 value to unpack mycsv file rows are like these:

2880,HY13KZV,2014/07/07 09:34:28,2014/07/08 09:34:28,1280
2880,RK09UZB,2014/07/07 10:34:05,2014/07/07 16:34:05,640
for row in reader:
                print row
                try:
                    (machine_name,vrm,startdatetime,enddatetime,ticket_price) = [x.decode('utf-8-sig') for x in row]
                except:
                    print "Error with row: " % row
                tmp = startdatetime.split(" ")
                tmp = enddatetime.split(" ")
                vrm = vrm.replace(' ','')
                vrm = vrm.upper()

                ticket_price = int( SessionCost / 100)

                date,time  = startdatetime.split('/')
                month,day,year = startdate.split('/')

                entryDatetime = "%s-%s-%s %s" % (year,month,day,time)

                date,time = enddatetime.split('/')
                month,day,year = enddate.split('/')

                expiryDatetime = "%s-%s-%s %s" % (year,month,day,time)

                sql_local = """INSERT INTO customer_1.pay_and_display
                    (plate, machine_id, ticket_datetime, expiry_datetime, ticket_name, ticket_price)
                    VALUES ("%s", "%s", "%s", "%s", "%s", "%s") """ % (vrm, machine_name, entryDatetime, expiryDatetime, "Ringo", SessionCost)
            print sql_local
            cursor.execute(sql_local)

            curl = pycurl.Curl()
            body = Body()

Upvotes: 0

Views: 65

Answers (1)

beiller
beiller

Reputation: 3135

Your problem is splitting on "/" brings out 3 fields

year,month,day  = startdatetime.split('/')

Actually there is a bit more errors than that. Use the following in its place

date,time = startdatetime.split(' ')
year,month,day = date.split('/')
hour,min,second = time.split(':')

Upvotes: 1

Related Questions