I installed the Db2 ODBC driver in Windows 10 following these instructions. I extracted the driver folder in C:\tools\db2cli\ and run db2cli install -setup from the bin folder.
This seems to have worked fine, as the ODBC driver was registered in Windows:
But when I try to connect from Jupyter I get this error:
server = 'localhost'
database = 'mydb'
username = 'db2inst1'
password = 'abc123'
driver = 'IBM DB2 ODBC DRIVER - C_tools_db2cli'
conn = pyodbc.connect('DRIVER={' + driver +'};SERVER='
+ server + ';DATABASE=' + database + ';UID='
+ username + ';PWD=' + password + ';PROTOCOL=TCPIP;PORT=50000' )
Error Traceback (most recent call
last) in
4 password = 'abc123'
5 driver = 'IBM DB2 ODBC DRIVER - C_tools_db2cli'
----> 6 conn = pyodbc.connect('DRIVER={' + driver +'};SERVER='
7 + server + ';DATABASE=' + database + ';UID='
8 + username + ';PWD=' + password + ';PROTOCOL=TCPIP;PORT=50000' )
Error: ('HY000', '[HY000] [IBM][CLI Driver] SQL1042C An unexpected
system error occurred. SQLSTATE=58004\r\n (-1042) (SQLDriverConnect);
[HY000] [IBM][CLI Driver] SQL1042C An unexpected system error
occurred. SQLSTATE=58004\r\n (-1042)')
I unsuccessfully tried to find the error codes to find a solution. What should be looked at to fix this problem?
Note: I added C:\tools\db2cli\bin to the PATH but still get the same error.
This issue was caused by a defect in clidriver for some specific configurations on Microsoft Windows.
You can workaround this by ensuring that the PATH does not have any other Db2-products on it (except for clidriver\bin), and then ensuring to add two additional directories to the PATH before launching python. These are the fully qualified pathnames to these directories:
clidriver\bin\icc64
clidriver\bin\amd64.vc12.crt
IBM should at some point deliver a fix for IT34327.
SERVER= is for the already cataloged database
otherwise you must use: HOSTNAME= if you want to specify full DSN.
https://cloud.ibm.com/docs/Db2onCloud?topic=Db2onCloud-con_program&locale=fr
Related
I am trying to connect to Oracle database in an on-premises VM from Databricks.
I tried to connect using CX_Oracle via Python through init-script by following this link. It failed with below errors
Then I tried to connect using Pyspark and it also failed with below error. Also installed OJDBC into the cluster, where I used OJDBC version compatible with Oracle DB version.
URL = "jdbc:oracle:thin:" + User_Name + "/" + Password + "#//" + IP + ":" + Port + "/" + DB_name
DbTable = DataBase_name + "." + Table_Name
empDF = spark.read.format("jdbc").option("url", URL).option("dbtable", DbTable).option("user", User_Name).option("password", Password).option("driver", "oracle.jdbc.driver.OracleDriver").load()
Using the same settings in ADF Lookup activity, using a self hosted IR I am able to establish the connection. Is there anything I am missing in the connection string from Databricks?
I'm currently running into the issue of being unable to connect to the a microsoft sql server database through python that I can connect to via Tableau. I'm attempting to track some historical data that we cannot keep within our database for future years. This uses windows authentication and I've confirmed it working from my local machine to the server in Tableau. However, I'm not sure what driver Tableau uses in order to make this connection. I have tried to connect via ODBC in Tableau and run into the same issue as I do in python.
import pyodbc
cnxn_write = pyodbc.connect(driver='{SQL Server}',
server='FQDN, Port Number\SQLEXPRESS',
database='DataAnalytics',
trusted_connection='yes'
)
Returns the following error code
---------------------------------------------------------------------------
OperationalError Traceback (most recent call last)
C:\Users\User\AppData\Local\Temp/ipykernel_6969/555555555.py in <module>
3 import pyodbc
4 import date
time
----> 5 cnxn_write = pyodbc.connect(driver='{SQL Server}',
6 server='server-name\SQLEXPRESS',
7 database='DataAnalytics',
OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC SQL Server Driver]Login timeout expired (0) (SQLDriverConnect)')
The following code returns the same error.,
import pyodbc
cnxn_write = pyodbc.connect(driver='{SQL Server}',
server='FQDN, Port Number',
database='DataAnalytics',
trusted_connection='yes'
)
Anyone have any thoughts on what driver I should be using to attempt to connect with this database? I've tried {SQL Server} and {ODBC Driver 17 for SQL Server}. Or is this more likely to be on the server side than on my end?
Thanks everyone!
So in case anyone else runs into this issue in the future. pyodbc was not properly connecting to the driver ODBC Driver 17 for SQL Server. My fix was creating a User DSN after running the Windows executable odbcad32.exe. That properly identified the server, credentials and mirror server for the database. I called this User DSN 'sqlexpress' and the following is my connect string now.
cnxn_write = pyodbc.connect(r'DSN=sqlexpress')
Sadly this feels more like a workaround than a solution, but it now properly connects to my database.
I had the same problem but this worked for me. I use mssql
import pymssql
conn = pymssql.connect(server='host:port', user='user', password='pass', database ='database')
#%%
cursor = conn.cursor()
cursor.execute('SELECT TOP 10 * from table;')
row = cursor.fetchone()
while row:
print(str(row[0]) + " " + str(row[1]) + " " + str(row[2]))
row = cursor.fetchone()
I am connecting to an Azure SQL database using pyodcb. We have multi-factor authentication enabled, so I am using ActiveDirectoryInteractive authentication. We also use single sign-on for Windows.
My code is as follows:
import pyodbc
driver = '{ODBC Driver 17 for SQL Server}'
server = 'xxxxxxxxxxxxx.database.windows.net'
database = 'xxxxxxxxxxxxx'
authentication = 'ActiveDirectoryInteractive'
username = 'xxxxxxxxxxxxx'
conn = pyodbc.connect(
'DRIVER=' + driver + ';' +
'SERVER=' + server + ';' +
'DATABASE=' + database + ';' +
'UID=' + username + ';' +
'AUTHENTICATION=' + authentication + ';')
cursor = conn.cursor()
When I run the script I get pop-ups to submit my password and MFA details. But the connection works whether I fill in these details or just close the prompts - suggesting that single sign-on is working successfully. So why am I getting the password and MFA pop-ups - and how can I suppress them?
I'm not sure if this will disable it in Office 365, but if you want to disable MFA for users in Azure ad, I think the easiest way is to disable it directly in the portal.
Trying to access Azure SQL through a python function in VS code, with Authentication set to Active Directory Integrated. Using pyodbc to connect.
Works fine when run locally but get an error after deploying to Azure. Also works fine if I use SQL login but I want to use Active Directory Integrated. I have already set myself as an AD admin.
What I am trying:
cnxn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};Server=tcp:khawajaserver1.database.windows.net,1433;Database=KhawajaDB1;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;Authentication=ActiveDirectoryIntegrated")
Error I get:
Result: Failure
Exception: Error: ('HY000', '[HY000] [Microsoft][ODBC Driver 17 for SQL Server]MAX_PROVS: Error code 0x57 (87) (SQLDriverConnect)')
Stack: File "/usr/local/lib/python3.6/site-packages/azure/functions_worker/dispatcher.py", line 308, in _handle__invocation_request
I know Active Directory Password as authentication type works.
db_list = [TEST_DB1, TEST_DB2]
sql_conn = None
for db in db_list:
try:
conn_string = 'DRIVER={ODBC Driver 17 for SQL Server};' \
'SERVER=' + <db_url> + \
';DATABASE=' + <db_name> + \
';UID=' + <db_username> + \
';PWD=' + <db_password> + \
';Authentication=ActiveDirectoryPassword'
print conn_string
sql_conn = pyodbc.connect(conn_string)
except Exception as e:
print "Exception:::", e
print 'Cannot connect to DB' + str(sys.exc_info()[0])
return None
sql_conn.cursor().execute(<some SQL Query>)
sql_conn.close()
when using the ODBC Driver 17 for SQL Server, the following works when you are using some form of Managed Identity to connect to an Azure SQL Instance;
conn_str = 'Driver={};SERVER=tcp:{},1433;DATABASE=CustomerProfiling;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;Authentication=ActiveDirectoryMsi;'.format("{ODBC Driver 17 for SQL Server}", os.environ["SQL_SERVER"])
conn = pyodbc.connect(conn_str)
The key is to use the ActiveDirectoryMsi Authentication attribute.
The following code worked for me
# Connection to SQL Server using AADIntegrated
import pyodbc
server = 'data1.database.windows.net'
database = 'MyTestDB'
authentication = 'ActiveDirectoryIntegrated'
kpi_server_connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';Authentication='+authentication+';TrustServerCertificate='+ 'no')
query_string = '''
select top 10 * from [SomeTable]
'''
df = pd.read_sql(query_string, kpi_server_connection)
df
Using the SQL Server Management Studio (SSMS) Express, I can find the database and connect without problems.
But when I use pyodbc to connect to the same server using:
import pyodbc
Server = r"xxxER\xxxSQLSERV"
db = "xxxDB"
user = "xxx"
password = "xxxx"
conn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server + ';DATABASE=' + db +';UID=' + user + ';PWD=' + password)
By Using Pyhton in my local i am able to connect but when i am trying in linux server getting below error
pyodbc.OperationalError: ('HYT00', u'[HYT00] [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
i tried with ODBC Driver 17 for SQL Server too but facing the same issue.Can any one please suggest me on this.
Microsoft's SQL Server ODBC drivers for Linux are unable to resolve instance names. You can use the free sqlserverport module to get the corresponding port number and use that to connect.
Try removing the instance from server and set it as
Server = "xxxER"
or if you have a port number append it as
Server = "xxxER,portNo"
I have this problem and solved my problem in here.
Just add version.
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server + ';DATABASE=' + db +';UID=' + user + ';PWD=' + password)
If not work change version 17 to 13 if not to 11 .
List versions of ODBC.
Check your mssql network config, my problem was there.
Enable port 1433 for all IPs.
Now is working with IP and host name.
I am using SSH Tunnel with remote server port forward to localhost,1433. This pyodbc connection intermittently fails if using the approach:
cnxn = pyodbc.connect('driver={ODBC Driver 17 for SQL Server};Server=localhost, 1433;'...
Replacing localhost with 127.0.0.1 seems to be working all the time
cnxn = pyodbc.connect('driver={ODBC Driver 17 for SQL Server};Server=127.0.0.1, 1433;'
Incidentally also did same using Ruby and FreeTDS, and Localhost,1433 worked all the time.. Must be something inside the pyodbc package or their approach that sometimes drop some info to make the DBMS on server fail to respond/timeout
This worked for me:
mssql://DRIVER={ODBC Driver 17 for SQL Server};SERVER={server_address,PORT};DATABASE=<my_database>;UID=<my_username>;PWD=<my_password>