Connecting to SQL Server Using pypyodbc

1.6k views Asked by At

I'm attempting to access an SQL in python using pypyodbc, here is the code I've got:

import pypyodbc as pyodbc
db_host = host
db_name = name
db_user = user
db_password = password
connection_string = ("DRIVER={SQL Server};SERVER=" + (db_host) + ";DATABASE=" + (db_name) + ";UID=" + (db_user) + ";PWD=" + (db_password) + ";Trusted_Connection=yes;")
db = pyodbc.connect(connection_string)

When I try connecting I get the following error:

pypyodbc.DatabaseError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied.')

I have tried changing the connection string in a number of different ways, but it still produduces the same error each time. This is work for a school project, and such I am trying to access the SQL server from the school system, and so the only thing I can think of that would mess it up is that there is a problem with the firewall and connection or something.

Any help or suggestions would be greatly appreciated. Cheers.

2

There are 2 answers

0
GoodWasHere On

Connection string is correct.
Check host, name, user and password.

1
Oxymoron88 On

If you are not particular about pyodbc, one working solution through pymssql:-

import pymssql
import _mssql

# Connect to SQL
db_host = host
db_port = port
db_user = user
db_password = password

Conn = pymssql.connect(server=db_host,user=db_user,password=db_password, port=db_port)