Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Execute .sql file in python
#3
Hello,

I modify your code and it goes well now.

try this:

def parse_sql(sql_file_path):
    with open(sql_file_path, 'r', encoding='utf-8') as f:
        data = f.read().splitlines()
    stmt = ''
    stmts = []
    for line in data:
        if line:
            if line.startswith('--'):
                continue
            stmt += line.strip() + ' '
            if ';' in stmt:
                stmts.append(stmt.strip())
                stmt = ''
    return stmts
Reply


Messages In This Thread
Execute .sql file in python - by pradeepkumarbe - May-27-2019, 07:18 AM
RE: Execute .sql file in python - by Gribouillis - May-27-2019, 09:59 AM
RE: Execute .sql file in python - by UFO - Nov-05-2019, 07:38 AM
RE: Execute .sql file in python - by MckJohan - Nov-05-2019, 08:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Failed to execute child process (No such file or directory) uriel 1 1,739 Sep-15-2022, 03:48 PM
Last Post: Gribouillis
  How To Set Up And Execute A Python File knight2000 4 2,089 May-31-2022, 10:02 AM
Last Post: Larz60+
  execute python script guy7200 1 1,662 Oct-25-2021, 09:55 PM
Last Post: Axel_Erfurt
  Possible to execute a python script before log off/shutdown with input commands? Kaltex 1 2,356 May-18-2021, 06:31 AM
Last Post: Skaperen
  Using python to execute app or cmd that requires admin credentials thewolf 0 2,167 Mar-05-2021, 08:15 PM
Last Post: thewolf
  Using a .bat file to execute simple anaconda commands in Windows 10 gacg1 0 4,827 Nov-30-2020, 03:24 PM
Last Post: gacg1
  Execute DBCC CHECKDB from python script susja 1 2,198 Aug-12-2020, 02:09 AM
Last Post: susja
  Trying to write and execute first Python script garvind25 3 3,003 May-23-2020, 07:36 PM
Last Post: garvind25
  How to assess elapsed time to execute a .exe file or opening a desktop application? Sudershan 2 2,219 Apr-18-2020, 01:40 PM
Last Post: buran
  How to execute Python code leemao 2 2,242 Dec-21-2019, 02:35 PM
Last Post: leemao

Forum Jump:

User Panel Messages

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