sql - 无法使用“read_sql”调用 SQL 查询类
问题描述
我正在尝试使用以下代码从数据库中提取结果:
import pandas as pd
import pyodbc
class DataManagement(object):
def __init__(self, database = None, server=None, trusted_connection=True, database_driver=ODBC_SQL2005_2012, uid=None, pwd=None):
self.server = server
self.database = database
self.uid = uid
self.pwd = pwd
# Use default server name none supplied - assumed to be localhost
if self.server is None:
self.server = SERVER
if self.database is None:
self.database = DATABASE
# Use default sql credentials if none provided
if self.uid is None or self.pwd is None:
self.uid = DEFAULT_UID
self.pwd = DEFAULT_PASSWORD
if trusted_connection:
self.connectionstring = "DRIVER={0};SERVER={1};DATABASE={2};Trusted_Connection=yes;".format(database_driver, self.server, self.database)
else:
self.connectionstring = 'DRIVER={0};SERVER={1};DATABASE={2};UID={3};PWD={4};'.format(database_driver, self.server, self.database, uid, pwd)
self.connection = pyodbc.connect(self.connectionstring)
self.cursor = self.connection.cursor()
def __enter__(self):
return self
def __exit__(self, ctx_type, ctx_value, ctx_traceback):
self.connection.commit()
self.connection.close()
qq = DataManagement()
sql =("select * from ***** ")
data_df = pd.read_sql(sql, qq)
我收到一个错误:
Traceback (most recent call last):
File "<ipython-input-94-453876631fe0>", line 3, in <module>
data_df = pd.read_sql(sql, qq)
File "***\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\sql.py", line 380, in read_sql
chunksize=chunksize)
File "***\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\sql.py", line 1468, in read_query
cursor = self.execute(*args)
File "***\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\sql.py", line 1426, in execute
cur = self.con.cursor()
TypeError: 'pyodbc.Cursor' object is not callable
我在 TypeError 看到了一个类似的问题:'pyodbc.Cursor' object is not callable (Python 3.6) but cannot get an answer from there。
请协助。
解决方案
我通过从
self.cursor = self.connection.cursor()
进入
self.cursor = self.connection.cursor
推荐阅读
- asp.net-core - ASP.NET Core WebApi EF:向用户添加角色
- java - 使用本机查询 Spring Boot 2 更新
- unit-testing - 找不到 SpringJUnit4ClassRunner.runleaf 方法
- python - 将特定文件从一个目录复制到另一个附加它的基本目录名称
- visual-studio - VSTS REST Api 创建任务
- python - 无法安装 setuptools
- javascript - 在 JSON 数组上应用多个过滤器时,只有第一个过滤器有效
- mysql - 基于选择查询mysql的更新和删除查询
- php - 在 PHP 中保护 $_GET 请求
- r - 计算表中的出现次数并在 rmarkdown 中显示,可扩展性