Python + Odbc:当我输入'%'之类的通配符时,我的查询不起作用

时间:2019-04-17 03:15:46

标签: python sql pyodbc

我尝试使用以下代码进行查询:

SELECT MES_User_ID, MES_User_Email_Address
FROM MES_User
WHERE MES_User_Email_Address LIKE 'excollege100%'

它什么也没返回。 我希望它能返回: 219730 excollegetest1006@gmail.com

尝试了以下内容:

1。)

SELECT MES_User_ID, MES_User_Email_Address
FROM MES_User
WHERE MES_User_Email_Address LIKE 'excollegetest1%'

它返回了我期望的值,但是我想通过加两个零进一步缩小它的范围,以便结果将返回100X系列。

2。)

SELECT MES_User_ID, MES_User_Email_Address
FROM MES_User
WHERE MES_User_Email_Address LIKE 'excollegetest1##%'

它什么也没返回

quser = r'excollegetest100%'

cursor = cnxn.cursor()
cursor.execute('''
SELECT MES_User_ID, MES_User_Email_Address
FROM MES_User
WHERE MES_User_Email_Address LIKE ?
''', quser)
row = cursor.fetchmany()

for row in cursor:
    print(str(row[0]) + " : " + row[1])

我什么也没退。

2 个答案:

答案 0 :(得分:0)

使用此

quser = 'excollegetest100'

sql='SELECT MES_User_ID,MES_User_Email_Address FROM MES_User WHERE 
MES_User_Email_Address LIKE %s'

args=[quser+'%']
cursor.execute(sql,args)
row = cursor.fetchall()

答案 1 :(得分:0)

import pyodbc

cnxn = pyodbc.connect("database")
cursor = cnxn.cursor()

qry = "excollege100"

sql = ('''SELECT MES_User_ID, MES_User_Email_Address 
    FROM MES_User 
    WHERE MES_User_Email_Address LIKE ?''')

param = f'{qry}%'
row = cursor.execute(sql, param)
row = cursor.fetchmany()

for row in cursor:
    try:
        print(str(row[0]) + " : " + row[1])
    except:
        print("TypeError")
        print (str(row))