Sharing .exe with people who do not have ODBC Driver installed? - python

I've created an Executable that utilizes pyodbc to connect to SQL Server. I need to share this executable with colleagues who have not downloaded the ODBC driver on their computers which causes the executable to fail to open.
I understand if they were to download the ODBC driver I could adjust the executable to detect the driver of the computer each time to avoid this issue. However, not everyone I share this executable with is technical so I would like to avoid having them install the ODBC driver.
Is there a way to create an executable that connects to SQL Server that does not require the user to have an ODBC driver installed on their computers?
try:
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
except:
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()

Windows includes an ODBC driver named "SQL Server" as part of its standard installation so every Windows machine has it. It is targeted to older versions of SQL Server so it doesn't support some of the newer features in the latest releases, but if your needs are fairly basic it might suffice.

Related

Pyodbc login time out error with (Azure) SQL Server

I am getting a strange error with Python condo environments and PyODBC. I have multiple Conda environments, I am able to connect from one of the environment (from a Linux machine) to SQL Server hosted on Azure but not from the other one. Both Python environments have version 3.7.7 of Python and version 4.0.0 of Pyodbc. Code is exactly the same and connection string uses SQL Server 17 driver.
conn_str='DRIVER={ODBC Driver 17 for SQL Server};SERVER=' +server+';Authentication=ActiveDirectoryPassword;DATABASE='+database+';UID='+self.user_name+';PWD='+self.password
Error is :
pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
As you can see above, the issue might be DNS-related. In the
connection string, try using an IP address instead of the hostname,
or check your DNS setup.
Try downgrade to ODBC driver 13 and have a try. For that version 17
needs to be uninstalled.
Try using connect from SQL Authentication instead of Active Directory
Password.
The SQL Server ODBC drivers for Linux from Microsoft are unable to determine instance names.

How to connect to Microsft SQL Sever 2005 in another pc using pyodbc?

I am trying to connect to a database, from a Windows 10, that is on another pc in the same local network using the pyodbc module. The external pc is a Windows Server 2003 and it has a database Microsoft SQL Server 2005. I am not sure how the connection string has to be, I have tried with the following code:
import pyodbc
DRIVER = "{ODBC Driver 11 for SQL Server}" # supports SQL Server 2005 through 2014
SERVER = "192.168.100.101" # the ip of the windows server 2003
PORT = "1433" # I am not sure if this parameter is needed
DATABASE = "database_name" # name of the database
USER = "user"
PASS = "psw"
string_connection = f"DRIVER={DRIVER};SERVER={SERVER};PORT={PORT};DATABASE={DATABASE};UID={USER};PWD={PASS}"
cnxn = pyodbc.connect(string_connection)
cursor = cnxn.cursor()
cursor.close()
cnxn.close()
But when I run the code above I get the following error:
InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Data source name not found and no default driver specified
This error message indicates the driver specified in the connection string is not installed on the client machine. Run odbcad32.exe on the Win10 machine and click the drivers tab to view the list of installed ODBC drivers.
To remediate, you can either change the connection string to use an already installed SQL Server ODBC driver, or install the ODBC Driver 11 for SQL Server from here.
Be aware that Windows Server 2003 and SQL Server 2005 have been out of support for several years. The oldest support Windows server version is 2012 and SQL 2012 is the oldest SQL version as of this writing. I suggest you migrate to newer versions.

How can I confirm that I have the correct ODBC Driver 17 for SQL Server?

I am trying to run the code below.
import pandas as pd
from sqlalchemy import create_engine
import urllib
#import pyodbc
params = urllib.parse.quote_plus("DRIVER='{ODBC Driver 17 for SQL Server}';SERVER=server.database.windows.net;DATABASE=my_db;UID=my_id;PWD=my_pwd")
myeng = sqlalchemy.create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
df.to_sql(name="dbo.my_table_name", con=myeng, if_exists='append', index=False)
I get an error when I hit the last line of code. I am getting this error.
DBAPIError: (pyodbc.Error) ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib ''{ODBC Driver 17 for SQL Server}'' : file not found (0) (SQLDriverConnect)")
I am reading through the documentation here.
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html
Everything pretty much makes sense to me, but I'm not sure how to reference the SQL Server driver. When I look at the DOBC setup on my laptop, I see this.
I think this is ok, but I'm actually pushing data to an Azure Data Warehouse (on a server machine, not my local machine). I'm not sure how to check the driver on that DB sitting on the server. Also, I'm not totally sure, but the problem seems to come from either the DRIVER or the SERVER. Basically, I am just looking for some guidance as to how to make this work. Thanks!
To check installed drivers or DSNs on client machine, use the following lists from pyodbc:
# LIST OF INSTALLED DATA SOURCES (DSNs)
print(pyodbc.dataSources())
# LIST OF INSTALLED DRIVERS
print(pyodbc.drivers())
Do note: 32 or 64-bit versions will only appear on the analogous bit-version of your Python installation (i.e., only 32-bit drivers will show on Python 32-bit and similarly for 64-bit). You show a list of 64-bit drivers but may be running Python 32-bit of which none of those are available. Recall Windows maintains two odbc executables usually in below system folders:
C:\Windows\System32\odbcad32.exe (your screenshot)
C:\Windows\SysWOW64\odbcad32.exe

Pyodbc driver/DSN not found AWS lambda - works on EC2

I am trying to connect to SQL Server using PYODBC inside AWS Lambda. I set up an EC2 instance and installed all dependencies and packages needed. I am able to query SQL within EC2 but not within Lambda.
Seems like I am missing a config or a library to set up the connections when packaging. I have copied the SQL driver in /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.3.so.1.1 location. Copied the odbc.ini and odbcinst.ini files as needed. Copied the libtds and libodc* as well.
The error that I get when I package all the libraries and dependencies into lambda package is below:
[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)
My odbc.ini and odbcinst.ini is below:
odbc.ini
[DEV]
Driver = ODBC Driver 17 for SQL Server
Description = DEV
Trace = No
Server = abc.net,1234
odbcinst.ini
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.3.so.1.1
UsageCount=1
I tried providing the DSN and also explicitly mentioning the Driver and Server name in pyodbc.connect. Still no luck!
Any help or direction would be appreciated!
TIA
After digging through multiple blogs and links, the answer is as simple as explicitly mentioning the location of driver when you connect using pyodbc.
conn = pyodbc.connect('Driver=msodbcsql17/lib64/libmsodbcsql-17.3.so.1.1;Server=server,port;UID=uname;PWD=pwd;database=db_name;Encrypt=YES;TrustServerCertificate=Yes')

Can't open database with pyodbc

I'm sure this is a stupidly simple issue but I just can't sort it out. I'm trying to open a local database with pyodbc but it won't let me.
The database and script are on the same path, there are no passwords. I only have one installation of Python on my system - Anaconda 3, and this is where pyodbc installed itself when I used PIP to install the Python 3.5, 64 bit whl file from Christoph Gohlke
import pyodbc
pyodbc.connect('Driver={SQL Server};Server=(local);Database=tblGrid.mdb;Trusted_Connection=yes;')
I get the following error:
pyodbc.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][Shared Memory]SQL Server does not exist or access denied. (17) (SQLDriverConnect)')
Thanks
You need to use the Microsoft Access ODBC driver to open an *.mdb file.
Here is an example of connecting:
pyodbc.connect(
r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\some\path\tblGrid.mdb')

Categories