1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| count = 1 countLine = 0
file_line = Lines()
class FoundException(Exception): pass
try: while(1): sql_insert = """insert into 0401 (taxi_id , passenger , time_h, time_m, time_s, lon, lat) values """ while(count < 5000): str_ori = f.readline() if (countLine * 5000 + count) == (file_line - 1): str_sp = str_ori.split(',') sql_insert += " ('{0}',{1},{2},{3},{4},{5},{6}),".format(str_sp[0],str_sp[1],str_sp[2],str_sp[3],str_sp[4],str_sp[5],str_sp[6]) commit2db(f, sql_insert) raise FoundException
str_sp = str_ori.split(',') sql_insert +=" ('{0}',{1},{2},{3},{4},{5},{6}),".format(str_sp[0],str_sp[1],str_sp[2],str_sp[3],str_sp[4],str_sp[5],str_sp[6]) count = count +1 commit2db(f, sql_insert) countLine += 1 print "load {0} lines".format(countLine * 5000) count = 1
except FoundException: print "the end of file"
def commit2db(f, sql_insert): str_ori = f.readline() str_sp = str_ori.split(',') sql_insert += " ('{0}',{1},{2},{3},{4},{5},{6})".format(str_sp[0],str_sp[1],str_sp[2],str_sp[3],str_sp[4],str_sp[5],str_sp[6]) cur.execute(sql_insert)
db.commit()
|