SLQ to Excel
by gomati444 - Monday February 24, 2025 at 05:39 AM
#1
How to convert SLQ file to Excel file free of cost ? please guide
Reply
#2
Which SQL server product?
Reply
#3
(02-24-2025, 05:39 AM)gomati444 Wrote: How to convert SLQ file to Excel file free of cost ? please guide

it's pretty easy to make it as a .csv or .tsv which will open in excel
Reply
#4
This Python script reads content from a .sql file and writes it to a .txt file. It can optionally strip out SQL comments and extra whitespace if desired, leaving a clean version of the SQL content. The script is designed to be run from the command line, making it flexible and easy to use.

Features
Read SQL file: Opens the source SQL file in read mode with appropriate encoding handling.
Optional cleaning: If requested via a command-line flag, removes SQL comments (-- ... and /* ... */) and unnecessary whitespace (extra spaces, blank lines).
Write to TXT file: Outputs the processed content to the specified text file (writing in UTF-8 encoding by default).
Error handling: Provides clear error messages for common issues (file not found, read/write errors, encoding problems).
Command-line usage: Accepts input and output file paths as arguments, and an optional flag for cleaning the content.
Implementation
Below is the Python code for the script. You can save this as a file (for example, sql_to_txt.py) and run it from the command line:

=======================
python name it as : python sql_to_txt.py

import argparse
import sys
import re

def remove_comments_and_whitespace(sql_content):
"""Remove SQL comments and unnecessary whitespace from content."""
# Remove block comments (/* ... */) using regex (DOTALL flag to include newlines).
sql_content = re.sub(r'/\*.*?\*/', '', sql_content, flags=re.DOTALL)
cleaned_lines = []
for line in sql_content.splitlines():
# Remove inline comments starting with --, then strip leading/trailing whitespace
cleaned_line = re.sub(r'--.*', '', line).strip()
if cleaned_line: # Only add non-empty lines
cleaned_lines.append(cleaned_line)
return '\n'.join(cleaned_lines)

def main():
parser = argparse.ArgumentParser(description="Convert an SQL file to a text file.")
parser.add_argument("input_file", help="Path to the source .sql file")
parser.add_argument("output_file", help="Path to the output .txt file")
parser.add_argument("--clean", action="store_true",
help="Remove SQL comments and unnecessary whitespace before writing output")
args = parser.parse_args()

# Read the SQL file content with encoding handling
try:
with open(args.input_file, 'r', encoding='utf-8') as f:
sql_content = f.read()
except FileNotFoundError:
print(f"Error: File '{args.input_file}' not found.")
sys.exit(1)
except UnicodeDecodeError:
# Fallback in case of encoding issues
try:
with open(args.input_file, 'r', encoding='latin-1', errors='ignore') as f:
sql_content = f.read()
except Exception as e:
print(f"Error reading '{args.input_file}': {e}")
sys.exit(1)
except Exception as e:
print(f"Error reading '{args.input_file}': {e}")
sys.exit(1)

# Optionally clean the content
if args.clean:
sql_content = remove_comments_and_whitespace(sql_content)

# Write content to the output file
try:
with open(args.output_file, 'w', encoding='utf-8') as f:
f.write(sql_content)
except Exception as e:
print(f"Error writing to '{args.output_file}': {e}")
sys.exit(1)

if __name__ == "__main__":
main()
=======================
To use the script, run it from the command line with the required arguments. For example:
> python sql_to_txt.py database.sql output.txt

This will read database.sql, remove all SQL comments and extra whitespace, and write the cleaned content to output.txt.
If you want to preserve comments and formatting, simply omit the --clean flag. For example:
> python sql_to_txt.py database.sql output.txt

This will copy the content of database.sql exactly into output.txt without modifications. The script will print an error message if the input file is not found or if any read/write issue occurs, ensuring it's easy to diagnose problems.


this si the general way and depte on your format you may adjust it using chatgpt or any ai Smile
Reply


Forum Jump: