【发布时间】:2018-05-05 03:06:13
【问题描述】:
我是 Python 和 SQL 服务器的新手。在过去的两天里,我一直在尝试将 pandas df 插入我们的数据库,但没有任何运气。谁能帮我调试错误。
我已经尝试了以下
import pyodbc
from sqlalchemy import create_engine
engine = create_engine('mssql+pyodbc:///?odbc_connect=DRIVER={SQL Server};SERVER=bidept;DATABASE=BIDB;UID=sdcc\neils;PWD=neil!pass')
engine.connect()
df.to_sql(name='[BIDB].[dbo].[Test]',con=engine, if_exists='append')
但是在 engine.connect() 行我收到以下错误
sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('08001', '[08001] [Microsoft][ODBC SQL Server Driver]Neither DSN nor SERVER keyword supplied (0) (SQLDriverConnect)')
谁能告诉我我错过了什么。我正在使用 Microsoft SQL Server Management Studio - 14.0.17177.0
我通过以下方式连接到SQL服务器
Server type: Database Engine
Server name: bidept
Authentication: Windows Authentication
for which I log into my windows using username : sdcc\neils
and password : neil!pass
这个我也试过了
import pyodbc
conn_str = (
r'Driver={SQL Server Native Client 11.0};'
r'Server=bidept;'
r'Database=BIDB;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
df.to_sql(name='Test',con=cnxn, if_exists='append')
我得到了这个错误
pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': ('42S02', "[42S02] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name 'sqlite_master'. (208) (SQLExecDirectW); [42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Statement(s) could not be prepared. (8180)")
任何帮助将不胜感激,因为我不知道该怎么做。
【问题讨论】:
标签: python sql-server pandas sqlalchemy pyodbc