'Can I pass strings with reserved characters?

When I try this:

db = 'ACS-Sociodemographics(USA,CensusBlockGroups,2018)'
ctx.cursor().execute(f"USE DATABASE {db}")

I get this error:

ProgrammingError: 001003 (42000): SQL compilation error:
syntax error line 1 at position 16 unexpected '-'.

Is there a recommended way to pass a string with reserved characters?



Solution 1:[1]

Yes, the db name needs to be encased in double quotes. To make life easier, wrap the whole thing in single quotes like so:

ctx.cursor().execute(f'USE DATABASE "{db}"')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brandon Lee Detty