尝试使用PYODBC将Access数据库表读入Pandas时出错
我想执行一个简单的任务,将MS Access数据库中的表数据以数据帧的形式引入Pandas。我最近的工作很好,现在我不明白为什么它不再工作了。我记得最初对连接进行故障排除时,我需要做的工作是安装一个具有正确位的新microsoft数据库驱动程序,因此我重新访问了该驱动程序,并重新安装了该驱动程序。下面是我使用的设置 笔记本电脑上的安装记录: 操作系统:Windows 7 Professional 64位验证2017年9月6日 访问版本:Access 2016 32位验证2017年9月6日 Python版本:Python 3.6.1 64位,使用>Python-V验证于2017年9月11日找到 所需的AccessDatabaseEngine将基于上述Python位 使用>AccessDatabaseEngine_X64.exe/passive verified 9/11/2017从2010版开始随AccessDatabaseEngine_X64.exe安装的Windows数据库引擎驱动程序 我正在运行以下简单的测试代码来尝试与测试数据库的连接尝试使用PYODBC将Access数据库表读入Pandas时出错,pandas,ms-access,pyodbc,Pandas,Ms Access,Pyodbc,我想执行一个简单的任务,将MS Access数据库中的表数据以数据帧的形式引入Pandas。我最近的工作很好,现在我不明白为什么它不再工作了。我记得最初对连接进行故障排除时,我需要做的工作是安装一个具有正确位的新microsoft数据库驱动程序,因此我重新访问了该驱动程序,并重新安装了该驱动程序。下面是我使用的设置 笔记本电脑上的安装记录: 操作系统:Windows 7 Professional 64位验证2017年9月6日 访问版本:Access 2016 32位验证2017年9月6日 Pyt
import pyodbc
import pandas as pd
[x for x in pyodbc.drivers() if x.startswith('Microsoft Access Driver')]
返回:
['Microsoft Access Driver (*.mdb, *.accdb)']
TestTable1
设置连接字符串
dbpath = r'Z:\1Users\myfiles\software\JupyterNotebookFiles\testDB.accdb'
conn_str = (r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};''DBQ=%s;' %(dbpath))
cnxn = pyodbc.connect(conn_str)
crsr = cnxn.cursor()
正在验证I是否已连接到db
for table_info in crsr.tables(tableType='TABLE'):
print(table_info.table_name)
返回:
['Microsoft Access Driver (*.mdb, *.accdb)']
TestTable1
尝试连接到TestTable1会出现以下错误
dfTable = pd.read_sql_table(TestTable1, cnxn)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-14-a24de1550834> in <module>()
----> 1 dfTable = pd.read_sql_table(TestTable1, cnxn)
2 #dfQuery = pd.read_sql_query("SELECT FROM [TestQuery1]", cnxn)
NameError: name 'TestTable1' is not defined
dfTable = pd.read_sql_table('TestTable1', cnxn)
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-15-1f89f9725f0a> in <module>()
----> 1 dfTable = pd.read_sql_table('TestTable1', cnxn)
2 #dfQuery = pd.read_sql_query("SELECT FROM [TestQuery1]", cnxn)
C:\Users\myfiles\Anaconda3\lib\site-packages\pandas\io\sql.py in read_sql_table(table_name, con, schema, index_col, coerce_float, parse_dates, columns, chunksize)
250 con = _engine_builder(con)
251 if not _is_sqlalchemy_connectable(con):
--> 252 raise NotImplementedError("read_sql_table only supported for "
253 "SQLAlchemy connectable.")
254 import sqlalchemy
NotImplementedError: read_sql_table only supported for SQLAlchemy connectable.
使用单引号重试会出现以下错误
dfTable = pd.read_sql_table(TestTable1, cnxn)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-14-a24de1550834> in <module>()
----> 1 dfTable = pd.read_sql_table(TestTable1, cnxn)
2 #dfQuery = pd.read_sql_query("SELECT FROM [TestQuery1]", cnxn)
NameError: name 'TestTable1' is not defined
dfTable = pd.read_sql_table('TestTable1', cnxn)
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-15-1f89f9725f0a> in <module>()
----> 1 dfTable = pd.read_sql_table('TestTable1', cnxn)
2 #dfQuery = pd.read_sql_query("SELECT FROM [TestQuery1]", cnxn)
C:\Users\myfiles\Anaconda3\lib\site-packages\pandas\io\sql.py in read_sql_table(table_name, con, schema, index_col, coerce_float, parse_dates, columns, chunksize)
250 con = _engine_builder(con)
251 if not _is_sqlalchemy_connectable(con):
--> 252 raise NotImplementedError("read_sql_table only supported for "
253 "SQLAlchemy connectable.")
254 import sqlalchemy
NotImplementedError: read_sql_table only supported for SQLAlchemy connectable.
我试着回到驱动程序的问题上,在没有任何运气的情况下重新安装32位版本
有人有什么想法吗?根据以下文件:
给定一个表名和一个SQLAlchemy可连接,返回一个数据帧。
此函数不支持DBAPI连接
由于pyodbc是DBAPI,请使用查询方法,con参数确实支持DBAPI:
dfTable = pd.read_sql("SELECT * FROM TestTable1", cnxn)
根据以下文件:
给定一个表名和一个SQLAlchemy可连接,返回一个数据帧。
此函数不支持DBAPI连接
由于pyodbc是DBAPI,请使用查询方法,con参数确实支持DBAPI:
dfTable = pd.read_sql("SELECT * FROM TestTable1", cnxn)
仅使用表名称读取db表
import pandas
from sqlalchemy import create_engine
engine=create_engine('postgresql+psycopg2://user:password@localhost/db_name')
df=pandas.read_sql_table("table_name",engine)
仅使用表名称读取db表
import pandas
from sqlalchemy import create_engine
engine=create_engine('postgresql+psycopg2://user:password@localhost/db_name')
df=pandas.read_sql_table("table_name",engine)
问题是关于MS Access,而不是PostgreSQL的问题是关于MS Access,而不是PostgreSQL的问题