def __init__(self, file_name, tbl_name, tbl_type, settings_file = "../chris_home.xml", inc_header = False, start_line = 0, file_del = "auto", end_line = None, skip_last = 1): if (file_name == None or tbl_name == None): print "Must declare at least file to import. Start with -f FILE_NAME. Better luck next time!" else: s = settings_parser() settings = s.initialize(settings_file) if settings['type'] == 'MySQL': db_conn = db_connection(self, settings['connection'], settings['user'], settings['password'], settings['db_name']) else: print "not recognised connection type" if file_del <> "excel" and file_del <> "xml": fr = FileReader(file_name) if file_del == "auto": print "trying to discover file delimter" file_del = fr.discover_delimiter(starting_line = start_line) if inc_header == True: print "getting column headers from file" header_line = start_line header = fr.get_line(header_line, file_del) arr = [] for el in header: st01 = el.replace(" ", "_") arr.append(st01) header = arr print "reading content" if inc_header == True: content = fr.readTextToArrayList(file_del, st_line = start_line+1, skip_end_lines = skip_last) else: content = fr.readTextToArrayList(file_del, st_line = start_line, skip_end_lines = skip_last) # create table if needed if tbl_type == "new": print "creating new table" if inc_header == True: tbl_cr = table_creator(content, tbl_name, header = header) else: tbl_cr = table_creator(content, tbl_name) new_tbl_stmt = tbl_cr.return_newTableStmt() db_conn.cursor.execute(new_tbl_stmt) db_conn.con.commit() print "table %s created!" % tbl_name header = tbl_cr.return_header() ## try inserting the lines print "inserting data" counter = 1 for line in content: #Say something every 100 lines: if counter % 100 == 0: print "Done %(line_count)i lines" % {'line_count':counter} ### prepare the insert statement for the line i = 0 ins_stmt = "insert into " + tbl_name + " (" for t in range(len(line)): ins_stmt += header[t] if t+1 < len(line): ins_stmt += ", " ins_stmt += ") values (" for el in line: ins_stmt += " %s" if i+1 < len(line): ins_stmt += ", " i += 1 ins_stmt += ")" ### insert the line: try: db_conn.cursor.execute(ins_stmt, list(line)) except: print "This didn't work, error in line %(line_count)i" % {'line_count':counter} counter += 1 db_conn.con.commit() ## logging the import job #closing the db: if db_conn: db_conn.close()
def connect(self): #connex = connector.create_connection(self) spar = sp.settings_parser() spar.initialize("test")