SQL Formatter for Python is a Visual Studio Code extension that formats SQL query strings embedded in Python code using the sql-formatter
package. This extension supports both inline and multiline SQL queries, converting them into a standardized, readable format.
- Automatically format SQL strings in Python.
- Supports:
- SQL strings assigned to variables.
- Converts single-quoted strings to triple-quoted strings for readability.
- Adjusts indentation to align with your Python code.
- Works seamlessly with inline and multiline SQL queries.
query = "SELECT id, name FROM users WHERE active = 1 ORDER BY name"
query = '''SELECT
id, name
FROM
users
WHERE
active = 1
ORDER BY
name
'''
- Clone the repository or download the extension
.vsix
package. - Install the extension in VSCode:
- Go to the Extensions view (
Ctrl+Shift+X
). - Click on the
...
menu and select "Install from VSIX..." - Choose the
.vsix
file.
- Go to the Extensions view (
- Reload VSCode to activate the extension.
- Open a Python file in VSCode.
- Highlight an SQL string or place the cursor on a line containing an SQL query.
- Use the following methods to format the SQL:
- Keyboard Shortcut:
Ctrl+Shift+F
(orCmd+Shift+F
on macOS). - Command Palette:
- Press
Ctrl+Shift+P
(orCmd+Shift+P
on macOS). - Type
Format SQL in Python
and select the command.
- Press
- Keyboard Shortcut:
The keyboard shortcut is active only when:
- The language mode is set to
Python
.
If you want to customize the shortcut, you can do so in the VSCode keybindings.json
file.