Python &引用;未指定驱动程序名称";将数据帧写入SQL Server表
我正在尝试将熊猫的数据帧写入SQL Server表。以下是我的例子:Python &引用;未指定驱动程序名称";将数据帧写入SQL Server表,python,sql-server,pandas,sqlalchemy,pyodbc,Python,Sql Server,Pandas,Sqlalchemy,Pyodbc,我正在尝试将熊猫的数据帧写入SQL Server表。以下是我的例子: import pyodbc import pandas as pd import sqlalchemy df = pd.DataFrame({'MDN': [242342342] }) engine = sqlalchemy.create_engine('mssql://localhost/Sandbox?trusted_connection=yes') df.to_sql('Test',engine, if_exists
import pyodbc
import pandas as pd
import sqlalchemy
df = pd.DataFrame({'MDN': [242342342] })
engine = sqlalchemy.create_engine('mssql://localhost/Sandbox?trusted_connection=yes')
df.to_sql('Test',engine, if_exists = 'append',index = False)
我收到以下错误消息。有没有关于如何修复的想法
c:\python34\lib\site-packages\sqlalchemy\connectors\pyodbc.py:82: SAWarning: No driver name specified; this is expected by PyODBC when using DSN-less connections
"No driver name specified; "
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-25-78677a18ce2d> in <module>()
4 engine = sqlalchemy.create_engine('mssql://localhost/Sandbox?trusted_connection=yes')
5
----> 6 df.to_sql('Test',engine, if_exists = 'append',index = False)
7
8 #cnxn.close()
c:\python34\lib\site-packages\pandas\core\generic.py in to_sql(self, name, con, flavor, schema, if_exists, index, index_label, chunksize, dtype)
980 self, name, con, flavor=flavor, schema=schema, if_exists=if_exists,
981 index=index, index_label=index_label, chunksize=chunksize,
--> 982 dtype=dtype)
983
984 def to_pickle(self, path):
c:\python34\lib\site-packages\pandas\io\sql.py in to_sql(frame, name, con, flavor, schema, if_exists, index, index_label, chunksize, dtype)
547 pandas_sql.to_sql(frame, name, if_exists=if_exists, index=index,
548 index_label=index_label, schema=schema,
--> 549 chunksize=chunksize, dtype=dtype)
550
551
c:\python34\lib\site-packages\pandas\io\sql.py in to_sql(self, frame, name, if_exists, index, index_label, schema, chunksize, dtype)
1564 if_exists=if_exists, index_label=index_label,
1565 dtype=dtype)
-> 1566 table.create()
1567 table.insert(chunksize)
1568
c:\python34\lib\site-packages\pandas\io\sql.py in create(self)
646
647 def create(self):
--> 648 if self.exists():
649 if self.if_exists == 'fail':
650 raise ValueError("Table '%s' already exists." % self.name)
c:\python34\lib\site-packages\pandas\io\sql.py in exists(self)
634
635 def exists(self):
--> 636 return self.pd_sql.has_table(self.name, self.schema)
637
638 def sql_schema(self):
c:\python34\lib\site-packages\pandas\io\sql.py in has_table(self, name, schema)
1577 query = flavor_map.get(self.flavor)
1578
-> 1579 return len(self.execute(query, [name,]).fetchall()) > 0
1580
1581 def get_table(self, table_name, schema=None):
c:\python34\lib\site-packages\pandas\io\sql.py in execute(self, *args, **kwargs)
1465 cur = self.con
1466 else:
-> 1467 cur = self.con.cursor()
1468 try:
1469 if kwargs:
AttributeError: 'Engine' object has no attribute 'cursor'
生成以下错误:
c:\python34\lib\site-packages\sqlalchemy\connectors\pyodbc.py:82: SAWarning: No driver name specified; this is expected by PyODBC when using DSN-less connections
"No driver name specified; "
可能的问题是您尚未指定驱动程序,请尝试:
engine = sqlalchemy.create_engine('mssql+pyodbc://localhost/Sandbox?trusted_connection=yes')
这是基于您在顶部看到的警告消息:
c:\python34\lib\site-packages\sqlalchemy\connectors\pyodbc.py:82: SAWarning: No driver name specified; this is expected by PyODBC when using DSN-less connections
"No driver name specified; "
请注意,您也可以使用pymssql代替pyodbc,但MS建议使用后者
编辑
以下是有关如何使用/不使用DSN(数据源名称)连接的官方文档:
您需要指定要使用的ODBC和要使用的ODBC驱动程序
engine = sqlalchemy.create_engine('mssql+pyodbc://localhost/Sandbox?driver=SQL+Server+Native+Client+11.0')
受信任的连接是默认设置,因此您不需要指定它,尽管这样做应该不会有什么坏处。我知道这个问题已经回答了一段时间了,这只是一个警告,但是如果您正确地传输了所有内容,并且仍然发生此错误,则会很烦人 对于所有那些像我一样努力解决这个问题的人来说,你也可以直接在脚本中输入驱动程序,Pyodbc.py提供了这种可能性(第26-28行):
以上信息非常有用。下面的评论是我的合并版本,可以帮助新生在搜索过程中 #使用库pandas和pyodbc-如果不可用,请使用pip install命令根据版本安装库。这里使用的Python版本是3.7.8
import pandas as pd
from sqlalchemy import create_engine
import pyodbc
#This query will work for sql authentication
def mssql_engine():
engine = create_engine('mssql+pyodbc://type_username:type_password@type_servername_or_localhostname/type_database_name?driver=SQL+Server+Native+Client+11.0')
return engine
#This query will for windows authentication
#Note: Uncomment below code for windows authentication
#def mssql_engine():
#engine = create_engine('mssql+pyodbc://localhostname/db_name?driver=SQL+Server+Native+Client+11.0')
#return engine
query = 'select * from table_name'
#using pandas to read from sql and passing connection string as function
df = pd.read_sql(query, mssql_engine() )
#printing result
print(df)
因此,无法将数据帧转储到SQL Server的表中?您能否提供您的版本:MS SQL Server版本、Pandas版本、SQLAlchemy版本?请尝试
df.to_SQL('Test',engine.connect(),如果_exists='append',index=False)
@GordThompson因为Pandas 0.14这是不正确的做法:@user1700890,请看-我想它完全回答了您的问题我的环境稍有改变,但是原始代码和您的建议,我得到了相同的错误c:\python34\lib\site packages\sqlalchemy\connectors\pyodbc.py:82:SAWarning:未指定驱动程序名称;PyODBC在使用无DSN连接“未指定驱动程序名称;”时会出现这种情况。
根据此答案和sqlalchemy文档,只有当自定义:如果您不使用DSN,则需要驱动程序时,才需要驱动程序-请参阅问题中的最后一条错误消息。@cco非常感谢您的回答!有没有可能用字典的形式写呢。另外,如果我需要提供登录名和密码,我该如何做?将字符串中的localhost
更改为user:password@localhost
应该做正确的事情。如果不起作用,则追加;用户Id=我的用户名;密码=我的密码代码>应该可以做到这一点。我不知道你所说的“字典格式”是什么意思。@cco,再次感谢你!字典的意思是这样的:cnxn=connect(driver='{sqlserver}',Server='localhost',database='test',uid='me',pwd='me2')
# for non-DSN connections, this *may* be used to
# hold the desired driver name
pyodbc_driver_name = 'ODBC Driver 17 for SQL Server'
import pandas as pd
from sqlalchemy import create_engine
import pyodbc
#This query will work for sql authentication
def mssql_engine():
engine = create_engine('mssql+pyodbc://type_username:type_password@type_servername_or_localhostname/type_database_name?driver=SQL+Server+Native+Client+11.0')
return engine
#This query will for windows authentication
#Note: Uncomment below code for windows authentication
#def mssql_engine():
#engine = create_engine('mssql+pyodbc://localhostname/db_name?driver=SQL+Server+Native+Client+11.0')
#return engine
query = 'select * from table_name'
#using pandas to read from sql and passing connection string as function
df = pd.read_sql(query, mssql_engine() )
#printing result
print(df)