Python Forum
How do i read particular text from text file and update those values in MS SQL table
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do i read particular text from text file and update those values in MS SQL table
#19
plase, start using error tags, when post traceback.
in the above code you don't have many employees data, you pass only one employee data, so you cannot use execute many (or more correct - it doesn't make sense to use it.
you either parse all files and collect all data e.g. in list of lists or process/submit one file at a time.
the advantage of process all files and then submit is that you will insert all data or none. if you process one file at a time and you get error somewhere in the middle, then you will have to keep track what is already inserted and what not.
something like this (NOT TESTED)
import glob

def parse_line(line):
    for item in ['Emp No', 'Name', 'SAL']:
        line = line.replace(item, '')
    return [item.strip() for item in line.split(':') if item.strip()]

def process_file(file_name):
    with open(file_name) as f: # No need to specify 'r': this is the default.
        line = f.readlines()[6]
    return parse_line(line)

def write_db(data, server, database, password):
    connection_string = f'DRIVER=\{SQL Server Native Client 11.0\};SERVER={server};DATABASE={database};UID={username};PWD={password}'
    with pyodbc.connect(connection_string, autocommit=False) as cnxn:
        cur = cnxn.cursor()
        sql = "INSERT INTO test1 (EmpNo, Name, SAL) VALUES (?, ?, ?)"
        cur.executemany(sql, data)
        

pattern = 'C:/Test/Python/Mag/*.txt'   
files = glob.glob(pattern)
all_data = [parse(file_name) for file_name in files]
write_db(data=all_data, server=server, database=database, password=password) # replace srever, database, password with actual values
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: How do i read particular text from text file and update those values in MS SQL table - by buran - Nov-15-2018, 03:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  speed up getting embedding from bert model for large set of text veda 7 383 May-27-2024, 08:28 AM
Last Post: Pedroski55
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 380 May-03-2024, 07:23 AM
Last Post: Pedroski55
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 1,388 Apr-24-2024, 05:47 AM
Last Post: Bronjer
Brick Number stored as text with openpyxl CAD79 2 692 Apr-17-2024, 10:17 AM
Last Post: CAD79
  Sending a text from Python sawtooth500 2 338 Apr-14-2024, 01:56 PM
Last Post: sawtooth500
  very newbie problem on text file zapad 2 368 Apr-12-2024, 06:50 PM
Last Post: zapad
  help with scrolling text on RGB Matrix Foutsy 3 469 Apr-09-2024, 09:00 PM
Last Post: deanhystad
  Text parsing Arik 5 528 Mar-11-2024, 03:30 PM
Last Post: Gribouillis
  replace text in a txt cartonics 19 2,606 Jan-30-2024, 06:58 AM
Last Post: Athi
  Text conversion to lowercase is not working ineuw 3 583 Jan-16-2024, 02:42 AM
Last Post: ineuw

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020