python - Duplicate inserts to Database Table -
my code works perfectly. scans text file , retrieves user defined information , stores access. issue i'm having is when code ran again, inserts data has been inserted.
is there easy way fix this? 1 thing data have in common have timestamp. possible use prevent duplicate?
cur.execute("select * main_setup order id"); rows = cur.fetchall() # outfilename = "out4.txt" # regex pattern used extract timestamp file # search timestamps 2017-06-13-22.31.30.978293 dateregex_1 = r"[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}\.[0-9]{2}\.[0-9]{2}\.[0-9]+" dateregex_2 = r"[0-9]{4}-[0-9]{2}-[0-9]{2} \/ [0-9]+:[0-9]+:[0-9]+" # compile pattern regdexptrn_1 = re.compile(dateregex_1) regdexptrn_2 = re.compile(dateregex_2) field_names = '' # call inserttoaccess function insert access database cur.execute('select * lastran order id') tlran = cur.fetchall() def insertlastran(jobname,timestamp): print(jobname,timestamp) def inserttoaccess(jobname, tableseq, timestamp, accesstablevalues, field_names): # try: params = (jobname, timestamp, tableseq, accesstablevalues[0], accesstablevalues[1], accesstablevalues[2], accesstablevalues[3], accesstablevalues[4], \ accesstablevalues[5], accesstablevalues[6], accesstablevalues[7], accesstablevalues[8], accesstablevalues[9], field_names) cur.execute("insert report_table (job_name,run_ts,seq_num,field1,field2,field3,field4,field5,field6,field7,field8,field9,field10,field11) values \ (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", params); conn.commit() # except: # conn.rollback() # extract current job fields def field_extract(filelines, fieldsarray, jobname, timestamp, delimit): # empty string in append # extracted fields matchstr = "" count = 0 tableseq = 0 accesstablevalues = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] field_names = '' line in filelines: field in fieldsarray: if field in line: key, value = line.split(delimit) matchstr += key.strip() + "\t\t : " + value.strip() + "\n" accesstablevalues[count] = value.strip() field_names += key.strip() + ';' count += 1 if count == 10: tableseq += 1 inserttoaccess(jobname, tableseq, timestamp, accesstablevalues, field_names) count = 0 accesstablevalues = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] field_names = '' if count > 0 , count < 10: tableseq += 1 inserttoaccess(jobname, tableseq, timestamp, accesstablevalues, field_names) # return string hold extracted fields # each field onn separate line return matchstr # open input , output files test_file = open(r'c:\users\cqt7wny\desktop\new\savers_rept_dt0712.txt', 'r+') # outfile = open(outfilename, 'w') # initialize used variables currentjobdata = [] startappending = false currentjobfound = false # gaurav note fields_to_extract = [] outfilestr = "" line in test_file: if startappending == false: # jobstart in job_start: if currentjobfound == false: # find job name current report , exit loop #====##########===== gaurav note rowx in rows: if rowx[1] in line: currentjobname = rowx[1] search_start_point = rowx[2] search_end_point = rowx[3] fields_to_extract = rowx[4].split(';') currentjobfound = true break if currentjobname == 'xx': currentjobname = previousjobname search_start_point = previous_search_start_point search_end_point = previous_search_end_point fields_to_extract = previous_fields_to_extract if search_start_point in line: startappending = true if startappending == true: currentjobdata.append(line) if len(search_end_point) > 1 , (search_end_point in line): # job end found, stop gathering lines startappending = false # time stamp # search in currnet line using # compiled regex pattern txt = "".join(currentjobdata) # find occurance of timestamps on current job lines timestamp = regdexptrn_1.findall(txt) # check timestamp found if len(timestamp) >= 1: # if there more 1 timestamp in current # job lines, first 1 timestamp = timestamp[0] else: timestamp = regdexptrn_2.findall(txt) if len(timestamp) >= 1: timestamp = timestamp[0] # append found output output string outfilestr += '########============ new job starts here ===========#########' outfilestr += "\n" outfilestr += "job# " + str(currentjobname) outfilestr += "\n" outfilestr += "timestamp: " + timestamp outfilestr += "\n" outfilestr = field_extract(currentjobdata, fields_to_extract, currentjobname, timestamp, ':') insertlastran(currentjobname,timestamp) print('current job name :', currentjobname, ' : ', timestamp) print(outfilestr) previousjobname = currentjobname previous_search_start_point = search_start_point previous_search_end_point = search_end_point previous_fields_to_extract = fields_to_extract currentjobname = 'xx' currentjobfound = false currentjobdata = [] fields_to_extract = [] search_start_point = ' ' search_end_point = ' ' test_file.close()
there can different possible ways can prevent duplicate inserts .
1.check if data inserted/present in database/table.if not exists insert data else ignore/do not insert data .
2.you can create unique constraint on think duplicacy exist .
like alter table mytable add constraint constraint1 unique(column1)
Comments
Post a Comment