SQL SELECT WHERE 列值为 = 字符串

SQL SELECT WHERE Col value is = string

目前我的 python 代码中有以下 T-SQL 语句:

otherExpense ='SELECT Master_Sub_Account, Debit, Credit FROM [dbo].[PostGL] as gl'\
         'INNER JOIN Accounts  '\
         'ON Accounts.AccountLink = genLedger.AccountLink '\
         'INNER JOIN _etblGLAccountTypes as AccountTypes '\
         'ON Accounts.iAccountType = AccountTypes.idGLAccountType '\
         'WHERE cAccountTypeDescription IS Other Expenses '\
         'AND genLedger.TxDate >  ? '\

    cursor = cnxn.cursor();
    cursor.execute(otherExpense ,[one_yrs_ago]);
    xAllOtherExpense = cursor.fetchall()
    cursor.close()
    otherExpenseX = []

    for row in xAllOtherExpense:
        rdict = {}
        rdict["Account"] = row[0]
        rdict["Debit"] = row[1]
        rdict["Credit"] = row[2]
        otherExpenseX.append(rdict)

我试过这段代码但出现错误

Incorrect syntax near 'Other'

我也尝试过将 "Other Expenses" 值作为参数,如下所示:

 'WHERE cAccountTypeDescription = ? '\

 cursor.execute(otherExpense ,['Other Expenses'] , [one_yrs_ago]);

那是行不通的,我也试过一个普通的等号,像这样:

 'WHERE cAccountTypeDescription = Other Expenses '\

 cursor.execute(otherExpense  , [one_yrs_ago]);

我不知道为什么 none 这些工作,有没有特定的方法来调用 SQL 语句中的字符串值?

字符串常量必须用单引号括起来。这意味着您应该为 Python 字符串使用双引号:

"WHERE cAccountTypeDescription = 'Other Expenses' "

参数化的应该是这样的;参数在单个列表中传递:

cursor.execute( otherExpense, ['Other Expenses', one_yrs_ago] )