使用 to_sql 将 pandas 数据帧导出到访问 table 生成错误
exporting pandas dataframe into a access table using to_sql generate error
我正在尝试使用 sqlalchemy-access 库将数据框插入访问数据库:
我的代码是:
import msaccessdb
import pyodbc
import pandas as pd
print(pyodbc.version)
db_file = r'database\sampledatabase.accdb'
input_csv_file= r'database\sample_data.csv'
print(input_csv_file)
msaccessdb.create(db_file)
cnxn_str = (
r'DRIVER={{Microsoft Access Driver (*.mdb, *.accdb)}};'
r'DBQ={};'
r'ExtendedAnsiSQL=1;'.format(db_file)
)
print(cnxn_str)
cnxn = pyodbc.connect(cnxn_str,autocommit=True)
input_data=pd.read_csv(input_csv_file)
input_data.to_sql('sample_table', cnxn, index=False, if_exists='replace')
cnxn.close()
但是当我 运行 这段代码时,我得到了这个错误:
Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': ('42S02', "[42S02] [Microsoft][ODBC Microsoft Access Driver] The Microsoft Access database engine cannot find the input table or query 'sqlite_master'. Make sure it exists and that its name is spelled correctly. (-1305) (SQLExecDirectW)")
当我试图运行这一行时产生错误:
input_data.to_sql('sample_table', cnxn, index=False, if_exists='replace')
这段代码有什么问题,我该如何解决?
编辑 1
根据评论和此 post,我将代码更改为:
import msaccessdb
import pandas as pd
from sqlalchemy import create_engine
import urllib
db_file = r'database\sampledatabase.accdb'
input_csv_file= r'database\sample_data.csv'
msaccessdb.create(db_file)
cnxn_str = (
r'DRIVER={{Microsoft Access Driver (*.mdb, *.accdb)}};'
r'DBQ={};'
r'ExtendedAnsiSQL=1;'.format(db_file)
)
params = urllib.parse.quote_plus(cnxn_str)
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
print(cnxn_str)
create_engine("access+pyodbc://@your_dsn")
input_data=pd.read_csv(input_csv_file)
input_data.to_sql('sample_table', engine, index=False, if_exists='replace')
但我仍然收到错误消息:
(pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC Microsoft Access Driver] Undefined function 'schema_name' in expression. (-3102) (SQLExecDirectW)")
[SQL: SELECT schema_name()]
(Background on this error at: http://sqlalche.me/e/14/f405)
编辑 2
所以代码现在看起来是这样的:
import msaccessdb
import pandas as pd
from sqlalchemy import create_engine
import urllib
db_file = r'database\sampledatabase.accdb'
input_csv_file= r'database\sample_data.csv'
msaccessdb.create(db_file)
connection_string = (
r'DRIVER={{Microsoft Access Driver (*.mdb, *.accdb)}};'
r'DBQ=database\sample_data.csv;'
r'ExtendedAnsiSQL=1;'
)
connection_uri = f"access+pyodbc:///?odbc_connect={urllib.parse.quote_plus(connection_string)}"
engine = create_engine(connection_uri)
input_data=pd.read_csv(input_csv_file)
input_data.to_sql('sample_table', engine, index=False, if_exists='replace')
但我仍然收到此错误:
(pyodbc.Error) ('IM012', '[IM012] [Microsoft][ODBC Driver Manager] DRIVER keyword syntax error (0) (SQLDriverConnect)')
(Background on this error at: http://sqlalche.me/e/14/dbapi)
请注意,我的系统上安装了Access。
驱动程序名称周围有两组大括号……
connection_string = (
r'DRIVER={{Microsoft Access Driver (*.mdb, *.accdb)}};'
r'DBQ=database\sampledatabase.accdb;'
r'ExtendedAnsiSQL=1;'
)
connection_uri = f"access+pyodbc:///?odbc_connect={urllib.parse.quote_plus(connection_string)}"
engine = create_engine(connection_uri)
... 但是因为您使用的是 r'string'
(不是 f'string'
),所以您应该只有一对大括号:
connection_string = (
r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
r'DBQ=database\sampledatabase.accdb;'
r'ExtendedAnsiSQL=1;'
)
connection_uri = f"access+pyodbc:///?odbc_connect={urllib.parse.quote_plus(connection_string)}"
engine = create_engine(connection_uri)
我正在尝试使用 sqlalchemy-access 库将数据框插入访问数据库:
我的代码是:
import msaccessdb
import pyodbc
import pandas as pd
print(pyodbc.version)
db_file = r'database\sampledatabase.accdb'
input_csv_file= r'database\sample_data.csv'
print(input_csv_file)
msaccessdb.create(db_file)
cnxn_str = (
r'DRIVER={{Microsoft Access Driver (*.mdb, *.accdb)}};'
r'DBQ={};'
r'ExtendedAnsiSQL=1;'.format(db_file)
)
print(cnxn_str)
cnxn = pyodbc.connect(cnxn_str,autocommit=True)
input_data=pd.read_csv(input_csv_file)
input_data.to_sql('sample_table', cnxn, index=False, if_exists='replace')
cnxn.close()
但是当我 运行 这段代码时,我得到了这个错误:
Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': ('42S02', "[42S02] [Microsoft][ODBC Microsoft Access Driver] The Microsoft Access database engine cannot find the input table or query 'sqlite_master'. Make sure it exists and that its name is spelled correctly. (-1305) (SQLExecDirectW)")
当我试图运行这一行时产生错误:
input_data.to_sql('sample_table', cnxn, index=False, if_exists='replace')
这段代码有什么问题,我该如何解决?
编辑 1
根据评论和此 post,我将代码更改为:
import msaccessdb
import pandas as pd
from sqlalchemy import create_engine
import urllib
db_file = r'database\sampledatabase.accdb'
input_csv_file= r'database\sample_data.csv'
msaccessdb.create(db_file)
cnxn_str = (
r'DRIVER={{Microsoft Access Driver (*.mdb, *.accdb)}};'
r'DBQ={};'
r'ExtendedAnsiSQL=1;'.format(db_file)
)
params = urllib.parse.quote_plus(cnxn_str)
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
print(cnxn_str)
create_engine("access+pyodbc://@your_dsn")
input_data=pd.read_csv(input_csv_file)
input_data.to_sql('sample_table', engine, index=False, if_exists='replace')
但我仍然收到错误消息:
(pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC Microsoft Access Driver] Undefined function 'schema_name' in expression. (-3102) (SQLExecDirectW)")
[SQL: SELECT schema_name()]
(Background on this error at: http://sqlalche.me/e/14/f405)
编辑 2
所以代码现在看起来是这样的:
import msaccessdb
import pandas as pd
from sqlalchemy import create_engine
import urllib
db_file = r'database\sampledatabase.accdb'
input_csv_file= r'database\sample_data.csv'
msaccessdb.create(db_file)
connection_string = (
r'DRIVER={{Microsoft Access Driver (*.mdb, *.accdb)}};'
r'DBQ=database\sample_data.csv;'
r'ExtendedAnsiSQL=1;'
)
connection_uri = f"access+pyodbc:///?odbc_connect={urllib.parse.quote_plus(connection_string)}"
engine = create_engine(connection_uri)
input_data=pd.read_csv(input_csv_file)
input_data.to_sql('sample_table', engine, index=False, if_exists='replace')
但我仍然收到此错误:
(pyodbc.Error) ('IM012', '[IM012] [Microsoft][ODBC Driver Manager] DRIVER keyword syntax error (0) (SQLDriverConnect)')
(Background on this error at: http://sqlalche.me/e/14/dbapi)
请注意,我的系统上安装了Access。
驱动程序名称周围有两组大括号……
connection_string = (
r'DRIVER={{Microsoft Access Driver (*.mdb, *.accdb)}};'
r'DBQ=database\sampledatabase.accdb;'
r'ExtendedAnsiSQL=1;'
)
connection_uri = f"access+pyodbc:///?odbc_connect={urllib.parse.quote_plus(connection_string)}"
engine = create_engine(connection_uri)
... 但是因为您使用的是 r'string'
(不是 f'string'
),所以您应该只有一对大括号:
connection_string = (
r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
r'DBQ=database\sampledatabase.accdb;'
r'ExtendedAnsiSQL=1;'
)
connection_uri = f"access+pyodbc:///?odbc_connect={urllib.parse.quote_plus(connection_string)}"
engine = create_engine(connection_uri)