简体   繁体   中英

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. 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 )

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.

You just got an url with a name and a dbname. 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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