0

I am trying to use string format() method to create a connection string.

Below is my code :

connStr = 'DRIVER={ODBC Driver 13 for SQL Server};SERVER={s};PORT=1443;DATABASE={d};UID={u};PWD={p}'.format(s = server, d = database, u = username, p = password)

It will get following error:

File ".\sqltest.py", line 9, in connStr = 'DRIVER={ODBC Driver 13 for SQL Server};SERVER={s};PORT-1443;DATABASE={d};UID={u};PWD={p}'.format(s = se rver, d = database, u = username, p = password) KeyError: 'ODBC Driver 13 for SQL Server\'

Hsing Yi
  • 163
  • 1
  • 2
  • 15
  • Probably with `\\`? – Nepho Dec 05 '17 at 09:13
  • 1
    Google search keywords I used to find the dupe: [`stackoverflow python string format escape brace`](https://www.google.com/search?q=stackoverflow+python+string+format+escape+brace). – user2357112 Dec 05 '17 at 09:15
  • use {{ }} try this `connStr = 'DRIVER={{ODBC Driver 13 for SQL Server}};SERVER={s};PORT=1443;DATABASE={d};UID={u};PWD={p}'.format(s = server, d = database, u = username, p = password)` – Wazy Dec 05 '17 at 09:18

0 Answers0