Trouble with SP_SEND_DBMAIL using pyodbc (error on except) - pyodbc

Error is not evident - not sure how to debug the except section
import pyodbc
driver='{ODBC Driver 17 for SQL Server}'
def exec_sqlcmd(sqlcmd,cnxn):
try:
cursor=cnxn.cursor()
cursor.exec(sqlcmd)
cnxn.commit()
cursor.close()
except:
print("debug in sql: " + sqlcmd)
server='tcp:server.domain.ou.ou'
database='mydb'
cnxn=pyodbc.connect(
Trusted_Connection='Yes',
Driver='{ODBC Driver 17 for SQL Server}',
Server='10.10.10.01,1433',
Database='mydb'
)
#cursor=cnxn.cursor()
profilename='serverdbmailprofilename'
recipients='me#mydomain.com'
subject='test me'
body='did you get this'
query="""Exec msdb.dbo.sp_send_dbmail
#profile_name=profilename,
#recipients=recipients,
#subject=subject,
#body=body"""
exec_sqlcmd(query,cnxn)
cnxn.close()
Error is not evident - not sure how to debug the except section - works fine in ssms and sends the email. Not a python superstar - looking for a way to debug the error in except section.
If you decide to paste and test - be sure to put the correct server name, domain name, email and so on to get a similar result.
Does the query require the three quotes? Error is different using just one single quote.
Doesn't appear the cursor needs to be pre-declared to work. There is an error from a script running similar queries (not this one) pointing to WinError 10060 indicating a peering issue in AWS. The peering appears to be correct and the security groups are open correctly. SQL server (through 1433 and subsequently pyodbc) should be working on the same port. SQL works but not pyodbc.
Is there a different port used by pyodbc that isn't port 1433?

I struggled with this error condition. In many examples, I see the short hand cursor.exec(sqlcmd) One modification made this example work.
cursor.execute(sqlcmd)
Having said that, I am still looking for the python way to output the SQL error.

Related

Excel ODBC Connection Issues and HY000 CURLError

I am trying to connect Excel to our Snowflake instance so that users can pull data into Snowflake. I've installed the latest ODBC driver and set the User and Server as required. The authenticator is set to externalbrowser. When we attempt to use that connection within Excel, one of two things occurs:
A bunch of browser tabs open but the user is able to connect and bring in data. Not sure why we have multiple tabs but at least they get what they need.
The connection just spins, and ultimately we end up with a HY000 error saying that the REST Request for our URL failed; code=52 msg=Server returned nothing (no headers, no data), oS code=2, osMsg='No such file or directory'.
We have tried multiple options and all of our other connections (JDBC for example) works just fine with the external browser setting. There doesn't seem to be much of a difference between users that can connect and those that can't.
There is a good Knowledge Base article written about connecting to Snowflake from Excel.
Skip Step 1: Configure SSO with Azure AD if you are not using SSO. Go on to Step 2 onward.
This is the article: https://community.snowflake.com/s/article/HOW-TO-connect-to-Snowflake-authenticating-with-Azure-AD-SSO-from-MS-Excel-ODBC-driver

The server has not found anything matching the requested URI - SSMS

I'm trying to connect to an Azure db using pyodbc and keep getting this error.
My connection string:
"Driver=ODBC Driver 17 for SQL Server;Server=company.database.windows.net;Authentication=ActiveDirectoryPassword;Database=My_Database;UID=DOMAIN\UserName;PWD=********;"
Each time I connect it returns error The server has not found anything matching the requested URI
The strange thing is that when I try to connect with these credentials from inside SSMS, the error I get is Windows logins are not supported in this version of SQL Server.
All the other questions with this error are on web apps and html pages, so I'm very stuck.
I figured it out.
The problem was that I was using UID=DOMAIN\UserName;. When I switched it to UID=username#domain.com; the connection worked.
Hope this helps someone else with the same problem - there are not many resources on this error.

Having trouble connecting to iSeries from .NET Core

Cross-posting with this on the IBM forums: https://www.ibm.com/mysupport/s/forumsquestion?id=0D50z00006egDnsCAE. Follow-up question located here: Having trouble connecting to iSeries from .NET Core
Hi all,
I'm very new to this whole thing, so let me know if there's any info which would help, that I'm not providing.
At the moment I'm just trying to get the very basics working - getting the connection to open. I have a stripped-down .NET Core project, which is simply exposing a button I can press that opens a connection for DB2. My code is as follows:
using IBM.Data.DB2.Core;
...
DB2Connection DB2Connection = new DB2Connection(connectionString);
DB2Connection.SystemNaming = true;
DB2Connection.Open();
My connection string is as follows:
"Server=###.###.###.###;Database=AAAA;UID=BBBB;PWD=CCCC;LibraryList=DDDD,EEEE;"
I'm getting the following exception:
IBM.Data.DB2.Core.DB2Exception (0x80004005): ERROR [08001] [IBM] SQL30081N A communication error has been detected. Communication protocol being used: "TCP/IP". Communication API being used: "SOCKETS". Location where the error was detected: "###.###.###.###". Communication function detecting the error: "connect". Protocol specific error code(s): "10061", "*", "*". SQLSTATE=08001
I really don't know how to proceed from here. For context - I'm using "IBM Navigator for i" to query the info directly, and that works just fine for the IP, User ID, and Password I used above.
I've done some reading up, and attempted a few different solutions, but none really helped. I did see that in "Integrating DB2 Universal Universal Database for iSeries with for iSeries with Microsoft ADO .NET", it suggested looking in the Work Management section of the navigator, and check under Server Jobs to see whether there was any added info - however, it does not appear there is anything there to see.
I do understand that I may require a license for this connection to work properly, and accept that if that ends up being the problem, I'll need to get the license - but I don't think I've reached that stage yet. For now I just want to make sure the connection itself works properly.
Any help or insights are greatly appreciated. Thank you.
The symptom SQL30081N with 10061 was resolved by including the PORT=xxx; in the connection string, where xxx is the correct TCP/IP port number on which the Db2-for-i-series is listening for connections.

ODBC connects, but MsAccess 2016 won't

Hello been struggling with this one:
Up until a few months ago I had an MSAccess 2010 database, it's matching SQL ODBC and life was good.
Then came MSAccess 2016 with a new laptop and now whenever I try to use the old database (and I've created fresh ones in the new version just to test), MsAccess will return an error:
a) SQL Server Error 53: About Failed Connection
b) SQL Server Error 17: Server does not exist or access is denied.
(I use linked tables and with queries I import the data needed)
What really complicates things to diagnose is that:
When you test the ODBC connectivity by itself, the test come back "successful".
I tried pairing the ODBC with Excel, and it did get through, I could see the table list to link.. so again, the ODBC works.
If I log into the work's VPN (even while being connected to the work internal network)... MsAccess 2016 queries magically work as intended.
So, there is something I'm missing with Access itself and some network setting somewhere, but my own IT department is stumped.
Any ideas are welcome on what may be happening. Thanks!

iSeries (AS400) Output with ODBC connection

I am very new to AS400, and I am stuck. I have read documenation but cannot find what I need.
I have an odbc connection to an AS400 server. When I run this command I get an Outfile with everything I need:
CALL QSYS.QCMDEXC('DSPUSRPRF USRPRF(*ALL) OUTPUT(*OUTFILE) OUTFILE(CHHFLE/TEST3)', 0000000061.00000)
Instead of the results going to an outfile I need to receive the results of this command to my script that is connecting through odbc. If I change 'OUTPUT(*OUTFILE)' to 'OUTPUT(*)' I get no results when I try to 'fetchall()'.
Is there any way to get this information through the odbc connection to my script?
EDIT: I am on a linux server, in a python script using pyodbc to connect. I can run sql queries successfully using this connection, but I can't figure out how to get the results of a command to come through as some sort of record set.
I hope I'm interpreting what you're asking correctly. it looks like you're accessing user profile data and dumping it to a file. It looks like you then want to use the contents of that file in a script or something that's running on Windows. If that's the case:
In general, when accessing data in a file from the Windows world, whether through ODBC and VBScript, or .NET, the AS/400 is treated like a database. All files in libraries are exposed via the built-in DB2 database. It's all automatic, and part of the Universal DB2 database.
So, after creating this file, you should have a file named TEST3 in library CHHFLE
You'd create a connection and execute the following SQL statement to read the contents:
Select * From CHHFLE.TEST3
This, of course, assumes that you have proper permissions to access this. You should be able to test this using the iSeries Navigator tool, which includes the ability to run SQL Scripts against the database before doing it in your script.
Added after reading comments above
There's info at this question on connecting to the DB2 from Python. I hope it's helpful.
OUTPUT(*) is not stdout, unfortunately. That means you won't be able to redirect OUTPUT(*) to an ODBC connection. Dumping to a DB2 table via OUTPUT(*OUTFILE) is a good plan. Once that's done, use a standard cursor / fetch loop as though you were working with any other DB2 table.

Resources