Connect SQL Server to Python 3 with pyodbc - sql

import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};SERVER=LENOVO-PCN;DATABASE=testing;')
cursor = cnxn.cursor()
cursor.execute("select Sales from Store_Inf")
row = cursor.fetchone()
if row:
print (row)
I try using python 3 with module pyodbc to connect SQL Server Express.
My codes gave a error:
('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]Named
Pipes Provider: Could not open a connection to SQL Server [2]. (2)
(SQLDriverConnect)')
Any idea for this?

Here is an example that worked for me using Trusted_Connection=yes
import pyodbc
conn_str = pyodbc.connect(
Trusted_Connection='Yes',
Driver='{ODBC Driver 11 for SQL Server}',
Server='SERVER_NAME,PORT_NUMBER',
Database='DATABASE_NAME'
)
connection = pyodbc.connect(conn_str)
Please note that port number is comma separated!

Related

How to query Hive from python connecting using Zookeeper?

I can connect to a Hive (or LLAP) database using pyhive and I can query the database fixing the server host. Here is a code example:
from pyhive import hive
host_name = "vrt1553.xxx.net"
port = 10000
connection = hive.Connection(
host=host_name,
port=port,
username=user,
kerberos_service_name='hive',
auth='KERBEROS',
)
cursor = connection.cursor()
cursor.execute('show databases')
print(cursor.fetchall())
How could I connect using Zookeeper to get a server name?
You must install the Kazoo package to query Zookeeper and find the host and port of your Hive servers:
import random
from kazoo.client import KazooClient
zk = KazooClient(hosts='vrt1554.xxx.net:2181,vrt1552.xxx.net:2181,vrt1558.xxx.net:2181', read_only=True)
zk.start()
servers = [hiveserver2.split(';')[0].split('=')[1].split(':')
for hiveserver2
in zk.get_children(path='hiveserver2')]
hive_host, hive_port = random.choice(servers)
zk.stop()
print(hive_host, hive_port)
Then just pass hive_host and hive_port to your Connection constructor:
connection = hive.Connection(
host=hive_host,
port=hive_port,
username=user,
kerberos_service_name="hive",
auth="KERBEROS",
)
And query as a standard python sql cursor. Here is using pandas:
df = pd.read_sql(sql_query, connection)

Having trouble connecting to server using SQL Alchemy and pymssql

I want to automate some SQL queries to save time, so I tried using SQL Alchemy to connect to the server.
conn = create_engine("mssql+pymssql:/<user>:<pass>#<address>/<database>")
query = pd.read_sql_query(<query>)
This gave me an unknown connection error, so I looked around and found pymssql
db= pymssql.connect(host=address, user=user, password=pass, database=db, port=1433)
I am now getting this error:
OperationalError: (20009), d'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (address:1433)\nNet-Lib error during Unknown error (10060)\n'
Any help would be appreciated.

MS SQL Server connection using ODBC Driver from Python [duplicate]

import pyodbc
connection = pyodbc.connect('Driver = {SQL Server};Server=SIWSQL43A\SIMSSPROD43A;'
'Database=CSM_reporting;Trusted_Connection=yes;')
Error:
connection = pyodbc.connect('Driver = {SQL Server};Server=SIWSQL43A\SIMSSPROD43A;'
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Do not put a space after the Driver keyword in the connection string.
This fails on Windows ...
conn_str = (
r'DRIVER = {SQL Server};'
r'SERVER=(local)\SQLEXPRESS;'
r'DATABASE=myDb;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
... but this works:
conn_str = (
r'DRIVER={SQL Server};'
r'SERVER=(local)\SQLEXPRESS;'
r'DATABASE=myDb;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
I am also getting same error. Finally I have found the solution.
We can search odbc in our local program and check for version of odbc. In my case I have version 17 and 11 so. I have used 17 in connection string
'DRIVER={ODBC Driver 17 for SQL Server}'
I'm using Django 2.2
and got the same error while connecting to sql-server 2012. Spent lot of time to solve this issue and finally this worked.
I changed driver to
'driver': 'SQL Server Native Client 11.0'
and it worked.
I've met same problem and fixed it changing connection string like below.
Write
'DRIVER={ODBC Driver 13 for SQL Server}'
instead of
'DRIVER={SQL Server}'
Local Ms Sql database server need or {ODBC driver 17 for SQL Server}
Azure Sql Database need{ODBC driver 13 for SQL SERVER}
Check installed drivers here => Installed ODBC Drivers
Format for connection to Azure Sql Database is :
import pyodbc
conn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};'
'SERVER=tcp:nameServer.database.windows.net,1433;'
'DATABASE=Name database; UID=name; PWD=password;')
Format for connection to Ms SQL Databse Local:
import pyodbc
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};'
'SERVER=server.name;' // example Doctor-Notebook\\MSSQLEXPRESS
'DATABASE=database.name; Trusted_connection = yes')
I faced this issue and was looking for the solution. Finally I was trying all the options from the https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Windows , and for my MSSQL 12 only "{ODBC Driver 11 for SQL Server}" works. Just try it one by one. And the second important thing you have to get correct server name, because I thought preciously that I need to set \SQLEXPRESS in all of the cases, but found out that you have to set EXACTLY what you see in the server properties. Example on the screenshot:
You could try:
import pyodbc
# Using a DSN
cnxn = pyodbc.connect('DSN=odbc_datasource_name;UID=db_user_id;PWD=db_password')
Note: You will need to know the "odbc_datasource_name". In Windows you can search for ODBC Data Sources. The name will look something like this:
Data Source Name Example
The below code works magic.
SQLALCHEMY_DATABASE_URI = "mssql+pyodbc://<servername>/<dbname>?driver=SQL Server Native Client 11.0?trusted_connection=yes?UID" \
"=<db_name>?PWD=<pass>"
Below connection string is working
import pandas as pd
import pyodbc as odbc
sql_conn = odbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER=SERVER_NAME;DATABASE=DATABASE_NAME;UID=USERNAME;PWD=PASSWORD;')
query = "SELECT * FROM admin.TABLE_NAME"
df = pd.read_sql(query, sql_conn)
df.head()
I have had the same error on python3 and this help me:
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};'
'SERVER=YourServerName;'
'DATABASE=YourDatabaseName;UID=USER_NAME;PWD=PASS_WORD;')
remember python is case-sensitive so you have to mention DRIVER,SERVER,... in upper case.
and you can visit this link for more information:
https://learn.microsoft.com/en-us/sql/connect/python/pyodbc/step-3-proof-of-concept-connecting-to-sql-using-pyodbc?view=sql-server-ver15
In my case, the exact same error was caused by the lack of the drivers on Windows Server 2019 Datacenter running in an Azure virtual machine.
As soon as I installed the drivers from https://www.microsoft.com/en-us/download/details.aspx?id=56567, the issue was gone.
for error : pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
No space between the driver and event
connection = Driver={SQL Server Native Client 11.0};
"Server=servername;"
"Database=dbname;"
"Trusted_Connection=yes;"
Apart from the other answers, that considered the connection string itself, it might simply be necessary to download the correct odbc driver. My client just faced this issue when executing a python app, that required it.
you can check this by pressing windows + typing "odbc".
the correct driver should appear in the drivers tab.
Create a DSN something like this (ASEDEV) for your connection and try to use DSN instead of DRIVER like below:
enter code here
import pyodbc
cnxn = pyodbc.connect('DSN=ASEDEV;User ID=sa;Password=sybase123')
mycur = cnxn.cursor()
mycur.execute("select * from master..sysdatabases")
row = mycur.fetchone()
while row:
print(row)
row = mycur.fetchone()`
I was facing the same issue whole day wasted and I tried all possible ODBC Driver values
import pyodbc
connection = pyodbc.connect('Driver = {SQL Server};Server=ServerName;'
'Database=Database_Name;Trusted_Connection=yes;')
In place of Driver = {SQL Server} we can try these option one by one or just you can use with you corresponding setting, somehow in my case the last one works :)
Driver={ODBC Driver 11 for SQL Server} for SQL Server 2005 - 2014
Driver={ODBC Driver 13 for SQL Server} for SQL Server 2005 - 2016
Driver={ODBC Driver 13.1 for SQL Server} for SQL Server 2008 - 2016
Driver={ODBC Driver 17 for SQL Server} for SQL Server 2008 - 2017
Driver={SQL Server} for SQL Server 2000
Driver={SQL Native Client} for SQL Server 2005
Driver={SQL Server Native Client 10.0} for SQL Server 2008
Driver={SQL Server Native Client 11.0} for SQL Server 2012
You need to download Microsoft ODBC Driver 13 for SQL Server
from Microsoft ODBC Driver 13
Thank you Avinash.
brilliant. I tried to connect to MS Azure database using PyCharm. It worked.
server = ''
database = ''
username = ''
password = ''
driver = 'SQL Server Native Client 11.0'
connection1 = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password + ';TDS_Version=8.0')
print("Connected.")
Try below:
import pyodbc
server = 'servername'
database = 'DB'
username = 'UserName'
password = 'Password'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute('SELECT * FROM Tbl')
for row in cursor:
print('row = %r' % (row,))
Have you installed any product of SQL in your system machine ?
You can download and install "ODBC Driver 13(or any version) for SQL Server" and try to run if you havent alerady done.
Make sure you have all drivers and db engine installed
https://www.microsoft.com/en-us/download/details.aspx?id=54920
server = '123.45.678.90'
database = 'dbname'
username = 'username'
password = 'pwork'
driivver = '{ODBC Driver 17 for SQL Server}'
samgiongzon='DRIVER='+driivver+';SERVER='+server+\
';DATABASE='+database+';UID='+username+\
';PWD='+password+';Trusted_Connection=no;'
pyodbc.connect(samgiongzon, autocommit=True)
it worked for me; you need to install driver from here
https://learn.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-ver15
or (in ubuntu) sudo apt-get install unixodbc-dev if you get an error with pip install pyodbc
if any one are trying to access the database which is hosted in azure then try to give the driver as ODBC Driver 17 for SQL Server

Connect R to SQL Server in MAC

I want to connect R to SQL Server on my MAC (El Capitan), I can do it very easy in Python, but in R I can't do it.
In Python it is easy as:
import pymssql
pymssql.connect(server = 'CHWN-DSX-DB02', user = 'XXXX',password ='XXXX',database = 'Info')
For R, I tried with RODBC library, but didn't work, I think the problem is the "Driver":
driver.name <- "SQL Server"
db.name <- "Info"
host.name <- "CHWN-DSX-DB02"
port <-""
server.name <-"XXX"
pwd <- "XXX"
# Use a full connection string to connect to a SAMPLE database
con.text <- paste("DRIVER=",RMySQL::MySQL(),
";Database=",db.name,
";Server=",host.name,
";Port=",port,
";PROTOCOL=TCPIP",
";UID=", server.name,
";PWD=",pwd,sep="")
con1 <- odbcDriverConnect(con.text)
This code in R never ends, and when I stop it, I have this warning:
Warning messages:
1: In odbcDriverConnect(con.text) :
[RODBC] ERROR: state 00000, code 0, message [iODBC][Driver Manager]dlopen(SQL Server, 6): image not found
There are several issues with your current setup:
General vs. Specific APIs: In Python you are using a specific SQL Server API: pymssql. In fair comparison to your R attempt, you should be using the ODBC API: pyodbc (to compare with RODBC). Remember there are multiple ways to connect to backend databases or data stores from client applications either by specific APIs or generalized APIs (ODBC, OLEDB, JDBC, etc.).
Required Drivers: To use any ODBC library be it Python, R, or other language, you need to have an installed ODBC driver on your client machine. Download such drivers before attempting connection. Nearly every RDBMS or data store maintains (usually free to download) ODBC drivers for Windows, Mac, and Linux OS's including SQL Server: 2013 or 2017.
Mixing R Libraries: In R, most APIs follow the DBI standard including ROracle, RJDBC, odbc, RMySQL, RPostgreSQL, RSQLite. Unfortunately, RODBC does not follow this standard. Your attempted connection appears to be an attempted DBI connection using the RMySQL::MySQL() object which even in DBI is not part of an ODBC connection string.
Note even though both require underlying ODBC drivers (see #2), RODBC is a different library and implementation than odbc. Additionally, do not conflate specific APIs such as RMySQL with an attempted general SQL Server ODBC. In fact, it is unclear why you use RMySQL when you have a variable for driver.name. Also such a value, 'SQL Server' is the Windows ODBC driver name and not macOS or Linux names. See below R ODBC connections:
RODBC
driver.name <- "ODBC Driver 13 for SQL Server" # REQUIRES DOWNLOAD
driver.name <- "ODBC Driver 17 for SQL Server" # REQUIRES DOWNLOAD
...
con.text <- paste0("driver=", driver.name,
";database=", db.name,
";server=", host.name,
";port=", port,
";protocol=TCPIP",
";UID=", user.name,
";PWD=", pwd)
conn <- odbcDriverConnect(con.text)
odbc
conn <- dbConnect(odbc::odbc(), driver = driver.name,
server = host.name, port = port, database = db.name
uid = user.name, pwd = pwd)
# ALTERNATIVE:
conn <- dbConnect(odbc::odbc(), .connection_string = con.text)

R and Oracle SQL

Have anyone tried to use R and Oracle servers?
The credientials required for connection are:
HOST = somewhere.com
PORT = 1525
SERVICE_NAME = someservice
USERNAME = user
PASSWORD = pass
I've tried RODBC:
library(RODBC)
channel <- odbcConnect("somewhere.com:1525/someservice", uid="user", pwd="pass")
But it only gives me these warnings:
Warning messages:
1: In odbcDriverConnect("somewhere.com:1525/someservice", uid="user", pwd="pass") :
[RODBC] ERROR: state IM010, code 0, message [Microsoft][ODBC Driver Manager] Data source name too long
2: In odbcDriverConnect("somewhere.com:1525/someservice", uid="user", pwd="pass") :
ODBC connection failed
Are there other ways to connect to Oracle?
Thanks all