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
#11
what the line 7 looks like (the output of the print)
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
#12
It's working.

Need some sql insert help (output values)..

which value i want insert in SQL...
Reply
#13
you have some decisions to make
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
#14
I tried the below code..

but, it is showing error.

what is the problem in the below code..

cnxn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cur = cnxn.cursor()
sql = "INSERT INTO test1 (EmpNo, Name, SAL) VALUES (%s, %s, %s)"
val = (Staff_no, Emp_name, emp_dept)
res = cur.execute(sql, val)
Reply
#15
(Nov-15-2018, 02:35 PM)ganeshsai2912 Wrote: but, it is showing error.
post the full traceback in error tags
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
#16
sorry. error is below.

---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
<ipython-input-49-e00dcd4f8f95> in <module>()
27 sql = "INSERT INTO test1 (Staffno, EmpName, Department) VALUES (%s, %s, %s)"
28 val = (Staff_no, Emp_name, emp_dept)
---> 29 res = cur.execute(sql, val)
30 cnxn.commit()

ProgrammingError: ('The SQL contains 0 parameter markers, but 3 parameters were supplied', 'HY000')
Reply
#17
you should use ?, not %s.
Also note about cursor.commit() and execute/executemany options

https://github.com/mkleehammer/pyodbc/wiki/Cursor
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
#18
Yes. i used executemany. below i the error

params = (Staff_no, Emp_name, emp_dept)
#res = cursor.executemany(sql, val)
res = cursor.executemany("insert into test1(Staffno, EmpName, Department) values (?, ?, ?)", params)
cursor.commit()

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-54-06ea60fdf959> in <module>()
28 params = (Staff_no, Emp_name, emp_dept)
29 #res = cursor.executemany(sql, val)
---> 30 res = cursor.executemany("insert into test1(Staffno, EmpName, Department) values (?, ?, ?)", params)
31 cursor.commit()

TypeError: ('Params must be in a list, tuple, or Row', 'HY000')
Reply
#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
#20
Thank You.

Is it possible to read one more item fro the text file..

currently i am using empno, name, sal.

along with this, i need to add two more parameter (relieve date,EmpDesignation,No.ofPrjhandled).

I thing below line is reading particular text from the text fille.
line = my_file.split('\n')[6]

if i want another parameter in another line how do we specify..?


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()]

my_file = """Please find below emp details,

EMP DETAILS
________________________________

Emp No: 101 Name:RASUL K SAL: 30000
________________________________

Join Date: 10 JAN 2010 From Native: First
Releave Date: 25 APR 2018 To Native: Second
EmpDesignation: Manager
No.ofProjects handled: 10


"""

line = my_file.split('\n')[6]
print(parse_line(line))


Is it possible to read one more item fro the text file..

currently i am using empno, name, sal.

along with this, i need to add two more parameter (relieve date,EmpDesignation,No.ofPrjhandled).

I thing below line is reading particular text from the text fille.

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()]

my_file = """Please find below emp details, 

EMP DETAILS 
________________________________

Emp No: 101 Name:RASUL K SAL: 30000 
________________________________

Join Date: 10 JAN 2010 From Native: First 
Releave Date: 25 APR 2018 To Native: Second 
EmpDesignation: Manager 
No.ofProjects handled: 10


"""

line = my_file.split('\n')[6]
print(parse_line(line))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 775 4 hours ago
Last Post: Bronjer
Brick Number stored as text with openpyxl CAD79 2 440 Apr-17-2024, 10:17 AM
Last Post: CAD79
  Sending a text from Python sawtooth500 2 175 Apr-14-2024, 01:56 PM
Last Post: sawtooth500
  very newbie problem on text file zapad 2 216 Apr-12-2024, 06:50 PM
Last Post: zapad
  help with scrolling text on RGB Matrix Foutsy 3 269 Apr-09-2024, 09:00 PM
Last Post: deanhystad
  Text parsing Arik 5 400 Mar-11-2024, 03:30 PM
Last Post: Gribouillis
  replace text in a txt cartonics 19 2,234 Jan-30-2024, 06:58 AM
Last Post: Athi
  Text conversion to lowercase is not working ineuw 3 471 Jan-16-2024, 02:42 AM
Last Post: ineuw
  Script that alternates between 2 text messages DiscoMatic 1 523 Dec-12-2023, 03:02 PM
Last Post: buran
  Recommended way to read/create PDF file? Winfried 3 2,887 Nov-26-2023, 07:51 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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