简体   繁体   English

我对使用sqlalchemy wih python的sql server有连接问题

[英]I have a connexion problem to sql server using sqlalchemy wih python

I want to connect into a sql server database using sqlalchemy and python. 我想使用sqlalchemy和python连接到sql服务器数据库。 but when I run the code below I get the following error 但是当我运行下面的代码时,出现以下错误

OperationalError: (pyodbc.OperationalError) ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. (-1) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (-1)') (Background on this error at: http://sqlalche.me/e/e3q8 ) OperationalError:(pyodbc.OperationalError)('08001','[08001] [Microsoft] [SQL Server Native Client 11.0] SQL Server网络接口:查找指定的服务器/实例[xFFFFFFFF]时出错。(-1)(SQLDriverConnect); [ 08001] [Microsoft] [SQL Server本机客户端11.0]登录超时已过期(0); [08001] [Microsoft] [SQL Server本机客户端11.0]建立与SQL Server的连接时发生了与网络相关或特定于实例的错误。找不到服务器或无法访问服务器。检查实例名称是否正确以及是否将SQL Server配置为允许远程连接。有关更多信息,请参见SQL Server联机丛书(-1)')(有关此错误的背景信息, 网址为: http: //sqlalche.me/e/e3q8

I think the problem has something to do with the driver, but I can't seem to understand it. 我认为问题与驱动程序有关,但我似乎无法理解。

here is my code: 这是我的代码:

  from sqlalchemy.sql import text
  from sqlalchemy import create_engine

  import secrets


  engine = create_engine( 'mssql+pyodbc://servername/test_db?       driver=SQL+Server+Native+Client+11.0')

  conn = engine.connect()

  s = text("SELECT * FROM user_tab ")
  result = conn.execute(s).fetchall()
  print(result)

Can you help me please! 你能帮我吗!

You're connecting to a DB without a user, password, hostname or port. 您正在连接到没有用户名,密码,主机名或端口的数据库。

When you look at the example in the documentation this is written: 当您查看文档中的示例时,将这样写:

# pymssql
engine = create_engine('mssql+pymssql://scott:tiger@hostname:port/dbname')

As you can see, this url contains way more information than you have in your url. 如您所见,此URL包含的信息比URL中包含的信息更多。

You just got an url with a name and a dbname. 您刚得到一个带有名称和dbname的URL。 I don't know what the first name represents.. 我不知道名字代表什么。

Most of the time the hostname will be localhost and the port 1434. But make sure where your mssqlserver is running and which user is allowed to make queries on the db! 大多数情况下,主机名将是localhost和端口1434。但是请确保mssqlserver在何处运行以及允许哪个用户在数据库上进行查询!

Documentation: https://docs.sqlalchemy.org/en/13/core/engines.html 文档: https : //docs.sqlalchemy.org/en/13/core/engines.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM