[英]Adaptive Server connection failed error in Python/Flask/sqlAlchemy environment with pymssql
Update: I've confirmed this is only a problem when using an Azure SQL instance. 更新:我已经确认这只是使用Azure SQL实例时的问题。 I can use the same conn string to connect to local, network, and remote SQL (AWS) instances - it is only failing when connecting to Azure. 我可以使用相同的conn字符串连接到本地,网络和远程SQL(AWS)实例-仅在连接到Azure时失败。 I can connect to the Azure instance with other tools, like Management Studio. 我可以使用其他工具(例如Management Studio)连接到Azure实例。
I am building a small Python(3.4.x)/Flask application. 我正在构建一个小的Python(3.4.x)/ Flask应用程序。 I'm a complete noob here so forgive me if I break any rules in posting. 我是一个完全菜鸟,因此,如果我在发帖时违反任何规则,请原谅我。
I have created the database engine with: 我用以下方法创建了数据库引擎:
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('mssql+pymssql://dbadmin:dbadminpass@somedomain.server.net/databasename?charset=utf8')
db_session = scoped_session(sessionmaker(autocommit = False, autoflush = False, bind = engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
import models
Base.metadata.creat_all(bind=engine)
Everything builds/interprets correctly at runtime but I get an error on running the query: 一切都在运行时正确构建/解释,但是在运行查询时出现错误:
usr = User.query.filter_by(username=form.user.data).first()
The error is: 错误是:
sqlalchemy.exc.OperationalError: (OperationalError) (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n') None None
packages are: Flask==0.10.1, pymssql==2.1.1, SQLAlchemy==0.9.8 软件包是:Flask == 0.10.1,pymssql == 2.1.1,SQLAlchemy == 0.9.8
Thanks in advance. 提前致谢。
I had similar problem and solved it by explicitly setting tds version = 7.0
. 我有类似的问题,并通过显式设置tds version = 7.0
。 FreeTDS reads the user's ${HOME}/.freetds.conf
before resorting to the system-wide sysconfdir/freetds.conf
. ${HOME}/.freetds.conf
在使用系统范围的sysconfdir/freetds.conf
之前,先读取用户的${HOME}/.freetds.conf
sysconfdir/freetds.conf
。 So, I created ~/.freetds.conf
with [global]
section as: 因此,我使用[global]
部分创建了~/.freetds.conf
:
[global]
tds version = 7.0
You can find more information on freetds.con
: http://www.freetds.org/userguide/freetdsconf.htm 您可以在freetds.con
上找到更多信息: http : freetds.con
As I just had the same problem. 因为我只是有同样的问题。
Since I could get pymssql
to connect bypassing sqlalchemy
, I figured everything else should be fine, so I used the create_engine
parameter connect_args
to pass everything straight to pymssql.connect
. 既然我能得到pymssql
连接绕过sqlalchemy
,我想一切要细,所以我用了create_engine
参数connect_args
直传球一切pymssql.connect
。
server_name = "sql_server_name"
server_addres = server_name + ".database.windows.net"
database = "database_name"
username = "{}@{}".format("my_username", server_name)
password = "strong_password"
arguments = dict(server=server_addres, user=username,
password=password, database=database, charset="utf8")
AZURE_ENGINE = create_engine('mssql+pymssql:///', connect_args=arguments)
This works fine and does not require one to meddle with the .freetds.conf
file at all. 这可以正常工作,根本不需要一个人.freetds.conf
文件。
Also, note that pymssql
requires usernname to be in the form username@servername
. 另外,请注意pymssql
要求usernname的格式为username@servername
。 For more information see the linked documentation. 有关更多信息,请参见链接的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.