I am trying to connect to Sybase using pyodbc.
conn = pyodbc.connect('DRIVER=/usr/lib64/libodbc.so;SERVER=DBName;DATABASE=Test;UID=username;PWD=password')
When i execute the above I get the following error.
Error: ('IM002', '[IM002] [unixODBC][SAP][ODBC Driver Manager] Unable to load resource file (-620) (SQLDriverConnect)')
I can connect to sybase using sqsh so the username and password are correct. Any other suggestions?
I am using Ubuntu 16.04. Not sure if that makes a difference.
try this please
import pyodbc
connection_string = "Driver=SQL Anywhere 17;Server=demo17;UID=dba;PWD=sql;DBN=demo"
connection_object = pyodbc.connect(connection_string)
cursor = connection_object.cursor()
sql_string = "select 1"
result = cursor.execute(sql_string)
counter = result.fetchone()[0]
print(counter)
connection_object.close()
I was using the incorrect driver.
It is a custom driver so I can't share.
Related
Whenever I tried using pyodbc to connect to a Rocket UniData/UniVerse data I kept running into the error:
pyodbc.Error: ('00000', '[00000] [Rocket U2][U2ODBC][0302810]Unable to allocate sufficient memory! (0) (SQLDriverConnect); [00000] [Rocket U2][U2ODBC][0400182]Connection not open. (0)')
My code looks as follows:
import pyodbc
conStr = 'Driver={U2 64-Bit ODBC};Database=myDb;Server=localhost;UID=user;PWD=password'
conn = pyodbc.connect(conStr)
cursor = conn.cursor()
I actually found that the simplest way to fix this error was to create a System DSN and then changed my code to the following:
import pyodbc
conStr = 'DSN=myTestDsn;UID=user;PWD=password'
conn = pyodbc.connect(conStr)
I am trying to connect mssql from python. For this I am using below code but looks like something is wrong with the connection. Can anyone help me ?
import sqlalchemy as sal
from sqlalchemy import create_engine
import pyodbc
##conn = pyodbc.connect('Driver={SQL Server Native client 11.0};server=localhost;database=Nifty;trusted_connection=yes;')
engine = sal.create_engine('mssql+pyodbc://localhost/Nifty?driver=SQL+Server+Native+client+11.0?Trusted_Connection=yes')
engine.execute('select top 2 * from [dbo].ABC')
I am getting below error
InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
(Background on this error at: https://sqlalche.me/e/14/rvf5)
pls first check if you have specified driver in connection:
control panel>Systems and Security>Administrative Tools.>ODBC Data Sources>System DSN tab>Add
and then try :
engine = sal.create_engine('mssql+pyodbc://localhost/Nifty?driver=SQL+Server+Native+client+11.0?Trusted_Connection=yes',echo = True)
official docs
or, You can use some of the Solutions below:
Solution 1
define driver like this :
Driver={ODBC Driver 17 for SQL Server};Server=serverName\instanceName;Database=myDataBase;Trusted_Connection=yes;
and then put it in pyodbc.connect(" here ") and run it using cursor, see this
Solution 2
With Windows Authentication Without using DSN's
engine = sal.create_engine('mssql+pyodbc://server/db')
Solution 3
using urllib
import urllib
params = urllib.parse.quote_plus("DRIVER={SQL Server Native Client 11.0};"
"SERVER=dagger;"
"DATABASE=test;"
"Trusted_Connection=yes")
engine = sa.create_engine("mssql+pyodbc:///?odbc_connect={}".format(params))
I'm trying to use Python to upload from a Pandas dataframe to a SQL Server table, but I can't successfully create a connection using sqlalchemy. I understand I first need to create an engine object using create_engine(), and create a connection object using engine.connect(), but no string I enter in create_engine() seems to work. I've tried the following:
engine = create_engine('mssql+pyodbc://myServer/myDB')
conn = engine.connect()
and:
engine = create_engine('mssql+pyodbc://Server=myServer;Database=myDB;')
conn = engine.connect()
and:
engine = create_engine('mssql+pyodbc://Driver={SQL Server};Server=myServer;Database=myDB;Trusted_Connection=yes;')
conn = engine.connect()
but all result in the following error:
InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I've also tried:
engine = create_engine('mssql+pyodbc://Driver={SQL Server Native Client 11.0};Server=myServer;Database=myDB;Trusted_Connection=yes;')
conn = engine.connect()
which results in the following error:
DBAPIError: (pyodbc.Error) ('IM010', '[IM010] [Microsoft][ODBC Driver
Manager] Data source name too long (0) (SQLDriverConnect)')
While I can successfully connect using pyodbc like this:
conn = pyodbc.connect('DRIVER={SQL Server};Server=myServer;Database=myDB;Trusted_Connection=yes;')
I can't seem to make this work for sqlalchemy.
Any help would be appreciated.
The solution:
engine = create_engine('mssql+pyodbc://ERRSTSDBP2/ActPri?driver=SQL+Server+Native+Client+11.0')
Thanks to norbeq for getting me most of the way there!
You can try connection like this:
engine = create_engine('mssql+pyodbc://user:password#host:port/myDB')
conn = engine.connect()
Anyone know how to solve this error? Trying to connect to Azure SQL Server.
Thanks a ton!
InterfaceError: (pyodbc.InterfaceError) ('IM002', u'[IM002]
[Microsoft][ODBC Driver Manager] Data source name not found and no
default driver specified (0) (SQLDriverConnect)') (Background on this
error at: http://sqlalche.me/e/rvf5)
Without your code, but just from the error message, it seams that there is some issue with your connection string.
You can use the code below for test:
import pyodbc
from sqlalchemy import create_engine
import urllib
params = urllib.quote_plus \
(r'Driver={ODBC Driver 13 for SQL Server};Server=tcp:yourDBServerName.database.windows.net,1433;Database=dbname;Uid=username#dbserverName;Pwd=xxx;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;')
conn_str = 'mssql+pyodbc:///?odbc_connect={}'.format(params)
engine_azure = create_engine(conn_str,echo=True)
print('connection is ok')
Hope it helps. And please let me know if any further issue.
I have a local DB on my machine called 'Test' which contains a table called 'Tags'. I am able to access this DB and query from this table through SQL Server management studio 2008.
However, when using pyodbc I keep running into problems.
Using this:
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost:1433;DATABASE=Test')
yields the error:
pyodbc.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]Invalid connection. (14) (SQLDriverConnectW); [01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Invalid Instance()). (14)')
(with or without specifying the port)
Trying an alternative connection string:
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost\Test,1433')
yields no error, but then:
cur = conn.cursor()
cur.execute("SELECT * FROM Tags")
yields the error:
pyodbc.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'Tags'. (208) (SQLExecDirectW)")
Why could this be?
I tried changing your query to
SELECT * FROM Test.dbo.Tags
and it worked.
I don't see any authentication attributes in your connection strings. Try this (I'm using Windows authentication):
conn = pyodbc.connect('Trusted_Connection=yes', driver = '{SQL Server}',
server = 'localhost', database = 'Test')
cursor = conn.cursor()
# assuming that Tags table is in dbo schema
cursor.execute("SELECT * FROM dbo.Tags")
For me, apart from maintaining the connection details (user, server, driver, correct table name etc.),
I took these steps:
Checked the ODBC version here (Windows 10) ->
(search for) ODBC ->
Select 32/64 bit version ->
Drivers ->
Verify that the ODBC driver version is present there. If it is not, use this link to download the relevant driver: here
Reference Link: here
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost:1433;DATABASE=Test')
This connection lack of instance name and the port shouldn't be writen like this.
my connection is this:
cn=pyodbc.connect('DRIVER={SQL Server};SERVER=localhost\SQLEXPRESS;PORT=1433;DATABASE=ybdb;UID=sa;PWD=*****')
enter image description here
Try replacing 'localhost' with either '(local)' or '.'. This solution fixed the problem for me.