我正在使用 Python 3.3、pypyodbc 1.2.1 和通过 Flexquarters QODBC 版本 14 访问的 Quickbooks Enterprise 12 公司文件。我是编程和 python 新手,所以仍在学习:) 我可以使用 pypyodbc 示例运行查询很好,并产生预期的结果。
注意执行中的硬编码电子邮件地址。这按预期工作:
def get_customer_id(search_col,search_str):
'''(str,str) --> str
>>>get_customer_id(email, foo@foo.com)
80000001-1385782702
'''
cur.execute("SELECT listid FROM CUSTOMER WHERE email='foo@foo.com'")
for row in cur.fetchall():
for field in row:
return field
如果我尝试使用从 pypyodbc 文档中读取的参数执行相同的操作,则会引发错误。我认为引号和参数标记有问题。
def get_customer_id(search_col,search_str):
'''(str,str) --> str
>>>get_customer_id(email, foo@foo.com)
80000001-1385782702
'''
cur.execute("SELECT listid FROM CUSTOMER WHERE email=?",(search_str,))
for row in cur.fetchall():
for field in row:
return field
试图变得更加蟒蛇?我真的很想重用该功能来搜索不同的列。就像是:
cur.execute("SELECT listid FROM CUSTOMER WHERE search_str=search_col")
我查看了其他一些线程,其中大多数似乎只是在处理参数,而不是要搜索的列。任何人都可以帮我学习这个吗?
PS忘记包括回溯:
Traceback (most recent call last):
File "C:\Users\Mike\Documents\Projects\qb_sync\quickbooks.py", line 32, in <module>
print(get_customer_id('email','foo@foo.com'))
File "C:\Users\Mike\Documents\Projects\qb_sync\quickbooks.py", line 27, in get_customer_id
cur.execute("SELECT listid FROM CUSTOMER WHERE email=?",[search_str,])
File "C:\Python\lib\site-packages\pypyodbc.py", line 1457, in execute
self._BindParams(param_types)
File "C:\Python\lib\site-packages\pypyodbc.py", line 1420, in _BindParams
check_success(self, ret)
File "C:\Python\lib\site-packages\pypyodbc.py", line 982, in check_success
ctrl_err(SQL_HANDLE_STMT, ODBC_obj.stmt_h, ret, ODBC_obj.ansi)
File "C:\Python\lib\site-packages\pypyodbc.py", line 960, in ctrl_err
raise Error(state,err_text)
pypyodbc.Error: ('HY004', '[HY004] [Microsoft][ODBC Driver Manager] SQL data type out of range')
[Finished in 1.7s]