在 pymssql 中正确传递具有“-”的数据库名称

Correctly pass a db name having '-' in pymssql

我有一个由“-”组成的数据库名称(如 dbName-2020)。当我尝试执行查询时出现以下错误:

pymssql.ProgrammingError: (102, b"Incorrect syntax near '-'.DB-Lib error message 20018, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n")

这是我使用的代码:

serverName = 'servername.com'
userName = 'user2'
passWord = 'pass2'
dbName = 'dbName-2020'
conn = pymssql.connect(serverName , userName , passWord, "tempdb")
cursor1 = conn.cursor()
db_query = "SELECT CONVERT(date,LastImportDate),* from "+dbName+"..tblStores WHERE run= %s"
query_result1 = cursor1.execute(db_query,('true'))
query_output = cursor1.fetchone()

相同的代码适用于所有其他数据库。如何传递带有“-”的数据库名称?

使用方括号

dbName = '[dbName-2020]'

What does the SQL Standard say about usage of backtick(`)?