我正在尝试使用 jaydebapi 包通过 python 脚本向 Teradata 数据库发送查询。在我尝试在类似声明中使用通配符之前,我过去没有遇到过提取数据的问题
SELECT .... FROM TABLE WHERE column LIKE 'value%'
JayDeBeApi说这个包提供了一个到数据库的 Python DB-API v2.0 连接。查看参数用法(并尝试格式化参数字符串的许多不同组合)我没有撤回任何记录。
但是,当我在 Teradata SQL Assistant 中运行此查询时,我确实找到了记录。
如何正确发送带有通配符参数的查询?
这是我的代码:
import jaydebeapi
import pandas as pd
from tabulate import tabulate
jars = ['C:\\Users\\<path to TD jars>\\tdgssconfig.jar', 'C:\\Users\\<path to TD jars>\\terajdbc4.jar']
user = 'USER_NAME'
password = "PASSWORD"
jclassname = 'com.teradata.jdbc.TeraDriver'
url = "jdbc:teradata://<URL_TO_DB>/LOGMECH=LDAP,TMODE=ANSI,CHARSET=UTF8"
driver_args = [url, user, password]
class Teradata(object):
def __init__(self):
self.conn = jaydebeapi.connect(jclassname, driver_args, jars)
self.cursor = self.conn.cursor()
def search_by_base_address(account):
td = Teradata()
address1 = account.base_address
city = account.city
state = account.state
q = '''
SELECT
BUSINESS_NAME,
SECONDARY_NAME,
STREET_ADDR,
STREET_ADDR2,
CITY,
STATE,
POSTAL_CD,
FROM DB.TABLE
WHERE STREET_ADDR LIKE ?
AND CITY = '{}'
AND STATE = '{}';
'''.format(city, state)
if len(address1) > 1:
result = pd.read_sql(q, td.conn, params=(address1+"%",))
print(tabulate(result, headers='keys', tablefmt='psql'))
else:
print('No base address to search')