I have a sql where that I have formatted with new lines and multiple tabs and spaces. I have this for example, but indented multiple times:
q = f'''
UPDATE
`table`
SET
`col1` = 1
WHERE
`another col` = 2
AND
`final col` = 3
'''
How can I convert the query into a single line?
CodePudding user response:
For IDLE editor, use Alt Q to bring selected code in one single line.
CodePudding user response:
SQL databases are not to fussy about SQL formatting. They will take multi-line or single line queries just fine. For readability (for humans), a multi-line string is often preferred.
Here's your one liner:
q = "UPDATE `table` SET `col1` = 1 WHERE `another col` = 2 AND `final col` = 3"
I kept your backticks ``, but I don't really know what they're for. Maybe some DBs use backtick for tables or columns that have spaces in their names?
CodePudding user response:
You can use
width = 100
textwrap.shorten(q, width)
giving
'UPDATE `table` SET `col1` = 1 WHERE `another col` = 2 AND `final col` = 3'
but you need to be careful if the SQL values contain multiple spaces, for example
SET `col1` = "a b c"
as they will be collapsed too.