Python+Odbc:当我放入通配符(如“%”)时,查询不起作用

2024-04-20 10:25:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

它什么也没回来。 我希望它会回来: 219730excollegetest1006@gmail.com你知道吗

尝试了以下操作:

1.)

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

它返回了我期望的结果,但是我想通过添加两个0来缩小它的范围,这样结果将返回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])

我什么都不退。你知道吗


Tags: 代码fromcomidaddressemailwhereselect
2条回答

用这个

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()
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))

相关问题 更多 >