Could not connect newly created SQL Server database using pyodbc - sql-server

I have configured a new SQL Server 2017.XXX instance in my Windows 10. Then, created a new database called CUSTOMER_DEVICES. I tried to connect to database using pyodbc. I could not and got below error:
Error - pyodbc.OperationalError: ('08001', u'[08001] [Microsoft][ODBC SQL Server Driver]
When I use the same code in existing production server it works. Code below
import pyodbc
conn = pyodbc.connect(r'Driver={SQL Server};Server=localhost;Database=CUSTOMER_DEVICES;Trusted_Connection=yes;')
cursor = conn.cursor()
print(cursor)

Check if SQL server Configuration Management is configured. Enable TCP, IP. Check below link Check here

Related

Which exact driver is sqlalchemy using?

I am having trouble with a MS SQL connection when using pyinstaller. When run in interactive mode, everything works as expected. After compiling to an exe, the MS SQL database connection times out on the first query with the following error:
(pyodbc.OperationalError) ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expire (0); ...
My connection string is similar to the following:
create_engine(
"mssql+pyodbc://USER:PASSWORD#SERVERIP/DB_NAME?driver=ODBC+Driver+17+for+SQL+Server"
)
In attempting to diagnose the issue, I am printing out the drivers available to pyodbc with pyodbc.drivers() (which shows a large disparity between available drivers in compiled vs interactive) as well as the driver in use using
print(session.bind.dialect.name)
> pyodbc
print(session.bind.dialect.driver)
> mssql
It returns the upper level python modules which are being used but not the .dll that is handling it at a lower level. Is there any way to find which exact driver is being used? Any tips on what could be causing the error in the compiled version in the firstplace would be appreciated as well.
The issue may be in your connection string.
To create a proper connection string to connect to MSSQL Server ODBC driver with sqlAlchemy use the following:
import urllib
from sqlalchemy import create_engine
server = 'serverName\instanceName,port' # to specify an alternate port
database = 'mydb'
username = 'myusername'
password = 'mypassword'
params = urllib.parse.quote_plus('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+password)
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
Also, you can check the following article Connecting to Microsoft SQL Server using SQLAlchemy and PyODBC
Is there any way to find which exact driver [.dll] is being used?
import pyodbc
cnxn = engine.raw_connection()
print(cnxn.getinfo(pyodbc.SQL_DRIVER_NAME)) # msodbcsql17.dll
print(cnxn.getinfo(pyodbc.SQL_DRIVER_VER)) # 17.08.0001

Can we create SQL Server connection from DBVisualiser using existing SQL JDBC driver?

I am new to DBvisualiser. I am working on private laptop, installed SQL Server Management Studio 18 and DBVisualiser. In SQL Server the connection is not encrypted and using windows authentication. TCP/IP enabled.
IN DBVisualizer I am using existing driver - JDBC
I get error
failed to login for user -***
what is missing here?

Pymssql Write to the server failed - 20006

I am using pymssql=2.1.1 to connect Azure database from python. Due to idle connection for few minutes, i am getting error (Write to the server failed) and not able to fetch the data.
I am using connect method of pymssql to create connection to Azure DB.
conn = pymssql.connect(server=v_host, user=v_user, password=v_passwd, database=v_db)
cursor = self.conn.cursor(as_dict=True)
cursor.execute(query)
The error looks like
(20006, b'DB-Lib error message 20006, severity 9:\nWrite to the server failed\nNet-Lib error during Connection reset by peer (104)\n')
If you want to us pymssql connect to Azure SQL database, make sure the following requirements:
Examples:
import pymssql
conn=pymssql.connect("xxx.database.windows.net", "username#xxx", "password", "db_name")
cursor = conn.cursor()
cursor.execute(query)
For more details, please see: Connecting to Azure SQL Database. Starting with version 2.1.1 pymssql can be used to connect to Microsoft Azure SQL Database. And you can troubleshoot the error by pymssql Frequently asked questions.
Another way, you also can try the pyodbc example:
import pyodbc
server = '<server>.database.windows.net'
database = '<database>'
username = '<username>'
password = '<password>'
driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute(query)
Here is the Azure document: Quickstart: Use Python to query an Azure SQL database.
Hope this helps.

Connecting to SQL Server using PHP, ODBC, and Windows authentication

I'm trying to use PHP to connect to an ODBC data source using Windows authentication. I can connect just fine to the server in SQL Server so I know it's running. When I try to run the command
$link = odbc_connect("my_odbc","","");
I get the error:
"Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user ''., SQL state 28000 in SQLConnect in C:\Users..."
I tried:
$link = odbc_connect("Driver={ODBC Driver 11 for SQL Server};
Server='my_odbc';Integrated Security=SSPI","","");
Which returned the message:
Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. , SQL state 08001 in SQLConnect in C:\Users..."
Not sure what I'm doing wrong.
my_odbc is a SQL Server (2008) on a different machine. I don't have admin privileges on that database so I can't change anything on that end (such as enabling SQL Server authentication).
I am running Windows 7 and using PHP Version 5.6.12
phpinfo() indicates that ODBC Support is enabled as well as pdo_sqlsrv support
Maybe you should try PDO (the performance difference isn't that great) with SQLsrv plugin (this what I'm using to connect to my other boss' software which use SQL Server 2008 database):
$connection = new PDO("sqlsrv:Server=" . $this->sourceServer . ";Database=" . $this->sourceDB, $this->sourceUser, $this->sourcePW);
$connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
You can download the plugin here
https://www.microsoft.com/en-ca/download/details.aspx?id=36434
In Xampp, you must copy the dll in that folder:
C:\xampp\php\ext
And you must add that line to your php.ini
extension = php_pdo_sqlsrv_56_ts.dll
Note: Don't forget to restart your server so it can take in account the new php.ini file.
Let me know if it works for you
This error is a General Error that arises due to unable to establish connect to the server for some reasons. Its important to know what server are you using with PHP and the application used with it. Example Apache, Xampp or Wamp etc.
Here are somethings you could try like.
Let me brief, below are the reference links.
Check whether it pings
Enable TCP/IP connection SQL Server Configuration if not enabled. To do so, Open SQL Server Configuration Manager -> SQL Server Network
Configuration->Protocols for SQL server->TCP/IP(set to enabled).
Restart SQL services.
Enable Remote Connection from server
Open the Port, To do so Windows Firewall Settings-> Exceptions -> add a Port (Name:SQL;Port:1433;TCP) then from Exceptions tick SQL and save.
Enable running browser services.SQL Server Configuration Manager -> SQL Server Services - > SQL Server Browser set to running.
defining Port in connection string
add instance name with machine name if more than one instance is being used
sample
$user = 'username';
$pass = 'password';
//Use the machine name and instance if multiple instances are used
$server = 'serverName\instanceName';
//Define Port
$port='Port=1433';
$database = 'database';
$connection_string = "DRIVER={ODBC Driver 11 for SQL Server};SERVER=$server;$port;DATABASE=$database";
$conn = odbc_connect($connection_string,$user,$pass);
here are useful links that i found regarding the issue
resolving could not open a connection to sql server-errors
sql server provider named pipes provider error
Have a look at these links too
Named Pipes Provider: Could not open a connection to SQL Server [53]
Someone had the same error message and asked and answered his own question here.
To quote...
I´m sorry for the troubles.
The problem was, that I was using SQL Server Native Client 11.0 as driver. I switched it to SQL Server and now it works :/
Hopefully this at least helps someone, being in a similar problem....
A significant omission from your question is how you are running PHP and with which credentials.
Assuming you are using IIS or Apache (or another web server), then your PHP process is probably running under the local system account:
Since this account is local it doesn't have any authorisation to access your remote SQL Server.
You could alter the IIS/Apache service to run with the credentials (yours?) that are authorised to connect to SQL Server, but be aware this might cause other permissions issues with the service as well as being a problem if you change your password in the future.
Try running a test script from the command line (which should run with your credentials by default) to determine where the problem is:
php -r "var_dump(odbc_connect(...));"
Look at the regedit HKLM/SOFTWARE/ODBC
what is your folder name ?
If there is a "SQL Server Native Client 11.0" then you have to write your php code like
odbc_connect("Driver={SQL Server Native Client 11.0};Server=ip;Database=db;", "user", "pass");
However some of the server like server 2008 will be record this odbc like
ODBC Driver 11 for SQL Server
Then you have to replace SQL Server Native Client 11.0 by ODBC Driver 11 for SQL Server.
You have to create an ODBC interface first. In principle, this should also work without a user and pw. If defined in interface.
$pdo = new PDO('odbc:dsnName','user','pw');
Enable in IIS on Manager PHP o PDO_ODBC:
I have authenticated by Windows with following PHP statement:
$ Conn = new PDO ("odbc: Driver = {SQL Server}; Server = JAMILI-PC \ SQLEXPRESS; null; null");
I am using Windows 2008.
I hope it solves your problem.

How to create a new ODBC connection to one remote SQL Server

I'm trying to run an application that I've developped recently. This one uses an ODBC connection to browse a defined database.
I'm trying to execute this app from a remote computer. Using VMWare, I've created a new Windows 7 environment and connected on. After that I've tried to create a new System DSN, but, when I set the logins that I'm using to connect on my local SQL Server 2008 I get
Connection failed: SQLState: '28000' SQL Server Error: 18452
[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed. The login
is from an untrusted domain and cannot be used with Windows
authentication.
How may I to deal with this, please?
Thanks a lot!
You will have to use SQL Server authentication to connect or add the local machine to the same domain where the server is.
http://msdn.microsoft.com/en-us/library/ms144284.aspx

Resources