I am a beginner at Data Science, taking up IBM Certification.
I need to complete this lab as my course content. However, I am getting the captioned error when I try to "Create the DB2 database connection" on Anaconda Jupyter Notebook.
My code is:
conn = ibm_db.connect("BLUDB", "scm36847", "XXX")
My ERROR Message says:
Exception: [IBM][CLI Driver] SQL1531N The connection failed because the name specified with the DSN connection string keyword could not be found in either the db2dsdriver.cfg configuration file or the db2cli.ini configuration file. Data source name specified in the connection string: "BLUDB". SQLCODE=-1531
**UPDATE: **
If you are talking about the following, I have done this:
conn = ibm_db.connect("BLUDB", "scm36847", "XXX")
print ("Connected to BLUDB: ", BLUDB, "as 9938aec0-8105-433e-8bf9-0fbb7e483086.c1ogj3sd0tgtu0lqde00.databases.appdomain.cloud: ", dsn_uid, "scm36847: ", dsn_hostname)
but then I get the error.
Related
I try to connect to SQL using the pyodbc package, but get the error:
SystemError:
built-in function connect returned NULL without setting an error
What could be the reason for this?
When I run the code from another computer - I do manage to connect to SQL.
The connection string I use:
conn_str = 'DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + database + ';'
conn = pyodbc.connect(conn_str)
I have ODBC and pyodbc installed on my computer.
Of course I set values for the database and server fields
Before I do connect I use a function that does logon with the username and password
Because of your question do not have a reproductible exemple i can't be 100% of what I will say;
This is probably a driver problem, if you have acces to db2dsdriver.cfg file or db2cli.ini you shoyld try to look in those files to find what is the exact name of the driver.
Following this could help you to have more information on the problem.
I've looked through some other things but haven't been able to find a working solution.
Here is my code:
conn = db.connect("Driver={SQL Server}; Server='Server';Database='Database_DW'; uid='uid'; pwd = 'pwd'")
I run this code and I get the following error:
DatabaseError: ('08001', '[08001] [Microsoft][ODBC SQL Server
Driver][DBNETLIB]SQL Server does not exist or access denied.')
I'm really at a loss here. I can log in fine through the SQL Server Client with the exact some credentials.
Consider adjusting connection strings as parameter values are not quoted. Right now, pypyodbc is attempted to find the 'Server' (quotes included) server.
conn = pypyodbc.connect("DRIVER={SQL Server};server=servername;database=databasename;" + \
"UID=username;PWD=***")
Alternatively, use keyword arguments:
conn = pypyodbc.connect(driver="{SQL Server}", host="servername", database="database",
uid="username", pwd="***")
I have a query that I can successfully run in the Microsoft SQL Server Management Studio app. It outputs data as I expected.
However, when I try to run the script via Python, it'll always throw an error.
I first created a generic sql reader function:
def sql_reader_single(qry_file, server_name, database):
server = db.connect(str('DRIVER={SQL Server};
SERVER='+server_name+';
DATABASE='+database+';'))
qry = open(qry_file, 'r').read()
data = pd.read_sql(qry, server)
return data
then I tried calling the above function to read my sql script:
dir = 'C:/Users/Documents/qry'
QryFile = os.path.join(dir, 'qry clean no comment.sql')
Data = sp.sql_reader_single(qry_file=QryFile, server_name='server1', database='db2')
And when I call the read.sql() function, I'd always get the following error:
': ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'l'. (102) (SQLExecDirectW)")
'1' is the last character in my query script.
So the error message is saying I have syntax error near the last character of my script.
Since the query does run successfully in SQL server studio, that eliminates the hypothesis that my query is in wrong syntax, which is what the error message suggests.
Here's a sample query that I tried running but would get the above error code:
SELECT distinct
[ShipmentId]
,shipmentstatus
FROM shipmentstatus
I also looked into many other links via Google search, but I am not getting any helpful tips. Any help is appreciated!
After trying to run a query on redshift, I get the following error message:
OperationalError: (psycopg2.OperationalError) server certificate for
"" does not match host name "".
I already installed the sqlalchemy-redshift package, as well as the Psycopg2 package. My code:
from sqlalchemy import create_engine
def run_query(query, connection_string):
red_engine = create_engine(connection_string)
data_set = pd.read_sql_query(query, red_engine)
return data_set
The exact same code does work on another computer, so we are sure that the login name, password and queries are correct, and the problem is specific to my computer. Any suggestions?
This worked for me in the end: we added a preferred sslmode to our code, as follows:
create_engine(connection_string, connect_args={'sslmode': 'prefer'})
I'm developing a script that it's supossed to read data from a Microsoft SQL database and display it in a nice format. Also, It's supossed to write into the database as well. The issue is that I'm not able to connect to the server.
I'm using this code:
import pymssql
server = "serverIpAddress"
user = "username"
password = "pass"
db = "databaseName"
port = 1433
db = pymssql.connect(server,user,password,port= port)
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print "Database version : %s " % data
# disconnect from server
db.close()
And I'm getting this traceback:
Traceback (most recent call last):
File ".\dbtest.py", line 9, in <module>
db = pymssql.connect(server,user,password,port= port)
File "pymssql.pyx", line 641, in pymssql.connect (pymssql.c:10824)
pymssql.OperationalError: (18452, 'Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.DB-Lib error message 20018, severity 14:\nGeneral SQL Server e
rror: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (serverip:1433)\n')
I've changed some data to keep privacy.
This give me some clues about what it's going on:
The login is from an untrusted domain and cannot be used with Windows authentication
But I don't know how to fix it. I've seen that some people uses
integratedSecurity=true
But I don't know if there is something like this on pymssql or even if that it's a good idea.
Also, I don't need to use pymssql at all. If you know any other library that can perform what I need, I don't mind changing it.
Thanks and greetings.
--EDIT--
I've also tested this code:
import pyodbc
server = "serverIpAddress"
user = "username"
password = "pass"
db = "databaseName"
connectString = "Driver={SQL Server};server="+serverIP+";database="+db+";uid="+user+";pwd="+password
con = pyodbc.connect(connectString)
cur = con.cursor()
cur.close()
con.close()
and I'm getting this traceback:
Traceback (most recent call last):
File ".\pyodbc_test.py", line 9, in <module>
con = pyodbc.connect(connectString)
pyodbc.Error: ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user '.\\sa'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Lo
gin failed for user '.\\sa'. (18456)")