PyOdbc 无法连接到 sql 服务器实例
PyOdbc fails to connect to a sql server instance
我正在尝试使用 pyodbc 版本 3.0.6 连接到 sql 服务器实例,SQLAlchemy 1.0.4 on Windows 7 使用 Python 2.7(32 位)。我使用的连接字符串如下
DRIVER={SQL Server};SERVER=mymachinename;DATABASE=mydb;UID=sa;PWD=admin1;
但我一直收到此错误
Could not parse rfc1738 URL from string 'DRIVER={SQL Server};SERVER=mymachinename;DATABASE=mydb;UID=sa;PWD=admin1'
我正在使用 sqlSoup.Something 这样的连接字符串
db = sqlsoup.SQLSoup(connectionstring)
编辑
我尝试使用 SqlAlchemy 中的 Automap,它也失败并显示相同的消息
Traceback (most recent call last):
File "C:\Sandbox\Python scripts\BumpValues\newConnectivityTest.py", line 7, in
engine = create_engine("DRIVER={SQL Server};SERVER=gagan;DATABASE=persons;UID=sa;PWD=admin1;")
File "C:\Python27\lib\site-packages\sqlalchemy\engine__init__.py", line 386, in create_engine
return strategy.create(*args, **kwargs)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\strategies.py", line 49, in create
u = url.make_url(name_or_url)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\url.py", line 176, in make_url
return _parse_rfc1738_args(name_or_url)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\url.py", line 225, in _parse_rfc1738_args
"Could not parse rfc1738 URL from string '%s'" % name)
sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'DRIVER={SQL Server};SERVER=gagan;DATABASE=persons;UID=sa;PWD=admin1;'
[Finished in 0.4s with exit code 1]
[shell_cmd: python -u "C:\Sandbox\Python scripts\BumpValues\newConnectivityTest.py"]
而我的代码如下
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine
Base = automap_base()
engine = create_engine("DRIVER={SQL Server};SERVER=gagan;DATABASE=persons;UID=sa;PWD=admin1;")
Base.prepare(engine, reflect=True)
usrs = Base.classes.users
print(usrs)
你能告诉我我做错了什么吗?
编辑
我建议这不是 Bryan 建议的问题的重复是 .例如,我遵循问题
中建议的相同示例
import sqlalchemy as sa
engine = sa.create_engine('mssql+pyodbc://sa:admin1@gagan/persons')
但即使它失败并显示相同的消息
这是快照..
第一个使用类型为“'mssql+pyodbc://user:password@server/database'”
的连接字符串
而第二个使用类型的连接字符串
'DRIVER={SQL Server};SERVER=gagan;DATABASE=people;UID=sa;PWD=admin1;
谢谢
Windows 生态系统中还有其他可用的驱动程序,您也可以尝试另外两个:)
There are actually two or three SQL Server drivers written and
distrubuted by Microsoft: one referred to as "SQL Server" and the
other as "SQL Native Client" and "SQL Server Native Client 10.0}".
DRIVER={SQL Server};SERVER=cloak;DATABASE=test;UID=user;PWD=password
DRIVER={SQL Native Client};SERVER=dagger;DATABASE=test;UID=user;PWD=password
DRIVER={SQL Server Native Client 10.0};SERVER=dagger;DATABASE=test;UID=user;PWD=password
参考:https://code.google.com/p/pyodbc/wiki/ConnectionStrings
编辑:1
由于 SQLSoup 是在 SQLAlchemy 之上编写的,因此您必须使用以下连接字符串:
"mssql+pyodbc://sa:admin1@mymachinename/mydb"
参考:http://docs.sqlalchemy.org/en/rel_0_8/dialects/mssql.html#module-sqlalchemy.dialects.mssql.pyodbc
好的,这似乎已经解决了问题
import urllib
connection_string = "DRIVER={SQL Server};Database=people;SERVER=gagan;UID=sa;PWD=admin1"
connection_string = urllib.parse.quote_plus(connection_string)
connection_string = "mssql+pyodbc:///?odbc_connect=%s" % connection_string
我正在尝试使用 pyodbc 版本 3.0.6 连接到 sql 服务器实例,SQLAlchemy 1.0.4 on Windows 7 使用 Python 2.7(32 位)。我使用的连接字符串如下
DRIVER={SQL Server};SERVER=mymachinename;DATABASE=mydb;UID=sa;PWD=admin1;
但我一直收到此错误
Could not parse rfc1738 URL from string 'DRIVER={SQL Server};SERVER=mymachinename;DATABASE=mydb;UID=sa;PWD=admin1'
我正在使用 sqlSoup.Something 这样的连接字符串
db = sqlsoup.SQLSoup(connectionstring)
编辑 我尝试使用 SqlAlchemy 中的 Automap,它也失败并显示相同的消息
Traceback (most recent call last): File "C:\Sandbox\Python scripts\BumpValues\newConnectivityTest.py", line 7, in engine = create_engine("DRIVER={SQL Server};SERVER=gagan;DATABASE=persons;UID=sa;PWD=admin1;") File "C:\Python27\lib\site-packages\sqlalchemy\engine__init__.py", line 386, in create_engine return strategy.create(*args, **kwargs) File "C:\Python27\lib\site-packages\sqlalchemy\engine\strategies.py", line 49, in create u = url.make_url(name_or_url) File "C:\Python27\lib\site-packages\sqlalchemy\engine\url.py", line 176, in make_url return _parse_rfc1738_args(name_or_url) File "C:\Python27\lib\site-packages\sqlalchemy\engine\url.py", line 225, in _parse_rfc1738_args "Could not parse rfc1738 URL from string '%s'" % name) sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'DRIVER={SQL Server};SERVER=gagan;DATABASE=persons;UID=sa;PWD=admin1;' [Finished in 0.4s with exit code 1] [shell_cmd: python -u "C:\Sandbox\Python scripts\BumpValues\newConnectivityTest.py"]
而我的代码如下
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine
Base = automap_base()
engine = create_engine("DRIVER={SQL Server};SERVER=gagan;DATABASE=persons;UID=sa;PWD=admin1;")
Base.prepare(engine, reflect=True)
usrs = Base.classes.users
print(usrs)
你能告诉我我做错了什么吗?
编辑 我建议这不是 Bryan 建议的问题的重复是 .例如,我遵循问题
中建议的相同示例 import sqlalchemy as sa
engine = sa.create_engine('mssql+pyodbc://sa:admin1@gagan/persons')
但即使它失败并显示相同的消息
这是快照.. 第一个使用类型为“'mssql+pyodbc://user:password@server/database'”
的连接字符串而第二个使用类型的连接字符串 'DRIVER={SQL Server};SERVER=gagan;DATABASE=people;UID=sa;PWD=admin1;
谢谢
Windows 生态系统中还有其他可用的驱动程序,您也可以尝试另外两个:)
There are actually two or three SQL Server drivers written and distrubuted by Microsoft: one referred to as "SQL Server" and the other as "SQL Native Client" and "SQL Server Native Client 10.0}".
DRIVER={SQL Server};SERVER=cloak;DATABASE=test;UID=user;PWD=password
DRIVER={SQL Native Client};SERVER=dagger;DATABASE=test;UID=user;PWD=password
DRIVER={SQL Server Native Client 10.0};SERVER=dagger;DATABASE=test;UID=user;PWD=password
参考:https://code.google.com/p/pyodbc/wiki/ConnectionStrings
编辑:1
由于 SQLSoup 是在 SQLAlchemy 之上编写的,因此您必须使用以下连接字符串:
"mssql+pyodbc://sa:admin1@mymachinename/mydb"
参考:http://docs.sqlalchemy.org/en/rel_0_8/dialects/mssql.html#module-sqlalchemy.dialects.mssql.pyodbc
好的,这似乎已经解决了问题
import urllib
connection_string = "DRIVER={SQL Server};Database=people;SERVER=gagan;UID=sa;PWD=admin1"
connection_string = urllib.parse.quote_plus(connection_string)
connection_string = "mssql+pyodbc:///?odbc_connect=%s" % connection_string