Python s:df_append.to_sql('FFIEC_CDR_Call_Schedule_RIBII',con=engine,if_exists='append')我得到这个错误:OperationalError:(sqlite3.OperationalEr
Python s:df_append.to_sql('FFIEC_CDR_Call_Schedule_RIBII',con=engine,if_exists='append')我得到这个错误:OperationalError:(sqlite3.OperationalEr,python,python-3.x,dataframe,pyodbc,Python,Python 3.x,Dataframe,Pyodbc,s:df_append.to_sql('FFIEC_CDR_Call_Schedule_RIBII',con=engine,if_exists='append')我得到这个错误:OperationalError:(sqlite3.OperationalError)table FFIEC_CDR_Call_Schedule_RIBII没有名为index的列,肯定有一个名为index的字段,但我不知道为什么需要它,因为它肯定不是字段。无论如何,我也尝试了以下方法:engine=create\u en
s:df_append.to_sql('FFIEC_CDR_Call_Schedule_RIBII',con=engine,if_exists='append')我得到这个错误:OperationalError:(sqlite3.OperationalError)table FFIEC_CDR_Call_Schedule_RIBII没有名为index的列,肯定有一个名为index的字段,但我不知道为什么需要它,因为它肯定不是字段。无论如何,我也尝试了以下方法:engine=create\u engine(cnxn,fast\u executemany=True)df\u append.to\u sql('FFIEC\u CDR\u Call\u Schedule\u RIBII',engine,index=False,如果存在,'append')这给了我这个错误:AttributeError:'pyodbc.Connection'对象没有属性'\u实例化\u插件'如果不将pyodbc.Connection对象传递给
create\u engine()
,则传递一个SQLAlchemy连接URI(字符串),SQLAlchemy为您创建DBAPI连接。再次检查。提示:您可以使用SQLAlchemy使用现有的pyodbc连接字符串。谢谢Gord。1433的TCP/IP端口已禁用。那没用。终于让它运行了,有了这个:engine=“mssql+pyodbc://Server_Name/DB_Name?driver=SQL 服务器本机客户端11.0?受信任的\u连接=是”
# first I loop through a few files and append everything to a list
# this works fine
# convert the list to a data frame
df_append = DataFrame(df_append)
df_append.shape
type(df_append)
# log into DB
import pyodbc
driver= '{SQL Server Native Client 11.0}'
conn_str = (
r'DRIVER={SQL Server};'
r'SERVER=LAPTOP-CEDUMII6;'
r'DATABASE=TestDB;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
cursor = cnxn.cursor()
cursor.execute('SELECT * FROM FFIEC_CDR_Call_Schedule_RIBII')
for row in cursor:
print('row = %r' % (row,))
# can log into the DB just fine...
# now I am trying to move the contents of the data frame to the table...
# Here is attempt #1...
df_append.to_sql('FFIEC_CDR_Call_Schedule_RIBII', cnxn, index=False, if_exists='replace')
# Error:
df_append.to_sql('FFIEC_CDR_Call_Schedule_RIBII', cnxn, index=False, if_exists='replace')
Traceback (most recent call last):
File "C:\Users\ryans\Anaconda3\lib\site-packages\pandas\io\sql.py", line 1681, in execute
cur.execute(*args, **kwargs)
ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'sqlite_master'. (208) (SQLExecDirectW); [42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (8180)")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<ipython-input-87-2d90babfc8a7>", line 1, in <module>
df_append.to_sql('FFIEC_CDR_Call_Schedule_RIBII', cnxn, index=False, if_exists='replace')
File "C:\Users\ryans\Anaconda3\lib\site-packages\pandas\core\generic.py", line 2615, in to_sql
method=method,
File "C:\Users\ryans\Anaconda3\lib\site-packages\pandas\io\sql.py", line 598, in to_sql
method=method,
File "C:\Users\ryans\Anaconda3\lib\site-packages\pandas\io\sql.py", line 1827, in to_sql
table.create()
File "C:\Users\ryans\Anaconda3\lib\site-packages\pandas\io\sql.py", line 721, in create
if self.exists():
File "C:\Users\ryans\Anaconda3\lib\site-packages\pandas\io\sql.py", line 708, in exists
return self.pd_sql.has_table(self.name, self.schema)
File "C:\Users\ryans\Anaconda3\lib\site-packages\pandas\io\sql.py", line 1838, in has_table
return len(self.execute(query, [name]).fetchall()) > 0
File "C:\Users\ryans\Anaconda3\lib\site-packages\pandas\io\sql.py", line 1693, in execute
raise ex from exc
DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': ('42S02', "[42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'sqlite_master'. (208) (SQLExecDirectW); [42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (8180)")
# Here is attempt #2...same error...
df_append.to_sql('FFIEC_CDR_Call_Schedule_RIBII', schema='dbo', con = cnxn)