I am trying to connect to Informix database using Python package ibm_db but am getting the below error.
Error:
builtins.Exception: [IBM][CLI Driver] SQL0902C A system error occurred. Subsequent SQL statements cannot be processed. IBM software support reason code: "". SQLSTATE=58005
SQLCODE=-902
Code:
import sys
import ibm_db
import ibm_db_dbi
import pyodbc
import subprocess
import os
import string
import pandas
try:
conn= ibm_db.connect("HOSTNAME=xxxxxx;PORT=1900;PROTOCOL=onsoctcp ;DATABASE=webrpt; INSTANCE=vec_sandbox;UID=xxxx;PWD=xxxx","","")
except:
print ("Transaction couldn't be completed:" , ibm_db.stmt_errormsg())
else:
print ("Transaction complete.")
I was able to connect using Pyodbc package and installing client SDK on the Linux maching and configuring /etc/odbc.ini , /etc/odbcinst.ini and /app/informix/etc/sqlhosts files.
thanks!!!
Related
Since the optuna documentation does not address which modules are required from MySQL, I installed everything of MySQL on my Windows 10 machine. I looked for MySQL on my PC (in which folder the installation takes place is not revealed during installation) and updated the Path variables to
C:\Program Files\MySQL\MySQL Server 8.0\bin
I have successfully created the mysqltestexample database.
Using python SQL connectors, I can reproduce the output using:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="Start123"
)
print(mydb)
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES")
for x in mycursor:
print(x)
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="Start123",
database="mysqltesteexample"
)
Connection to the mysqltesteexample does not raise an error - so everything seems to be fine. However, optuna is not able to connect to my database
My python script looks like this. It is the code from the optuna documentation, I just altered the name of the test database.
study0 = optuna.create_study(storage="mysql://root#localhost/mysqltesteexample",study_name="distributed-example")
study0 = optuna.create_study(storage="mysql+pymysql://root:Start123#localhost:3306/mysqltesteexample",study_name="distributed-example")
All attempts to modify the URL string according to https://docs.sqlalchemy.org/en/14/core/engines.html failed with the following error: ImportError: Failed to import DB access module for the specified storage URL. Please install appropriate one.
Can you please help me to get it done? Thank you in advance, please don't be too harsh.
Finally, I made it. I have to install some further packages from the cmd:
py -3.8 -m easy_install mysql-python
py -3.8 -m pip install mysqlclient
Python packages - as well documented as they are eyes rolling
I am trying to connect my python code to sql server. But there is an error which shows:
import pyodbc
ModuleNotFoundError: No module named 'pyodbc'
import pyodbc
cnxn = pyodbc.connect("Driver={SQL Server};"
"Server=localhost;"
"Database=SCMS2;"
"uid=sa;pwd=tazbirul94")
cursor = cnxn.cursor()
cursor.execute('SELECT * FROM Suppliers')
for row in cursor:
print('row = %r' % (row,))
Here is the pip freeze command:
This shows my odbc is present:
Here is the error
Here is my pycharm environment path
The pycharm is using a python from a .../venv/... folder while what you type in the cmd does not. Make sure to use the same python version for both.
One way would be to go to:
File->Settings->Project->Project Interpreter and change to the python that you have installed the module for
I am using Jupyter Notebooks to learn Python. I would like to connect to a MySQL db hosted locally hosted through MAMP. How would I approach this?
import os
import pymysql
import pandas as pd
host = os.getenv('MYSQL_HOST')
port = os.getenv('MYSQL_PORT')
user = os.getenv('MYSQL_USER')
password = os.getenv('MYSQL_PASSWORD')
database = os.getenv('MYSQL_DATABASE')
conn = pymysql.connect(
host=host,
port=int(3306),
user="root",
passwd=password,
db="[YOUR_DB_NAME]",
charset='utf8mb4')
df = pd.read_sql_query("SELECT * FROM YOUR_TABLE",
conn)
df.tail(10)
Assuming you have MySQL installed (instructions here for macOS using HomeBrew), you need to:
Install pip3 install ipython-sql
pip3 install mysqlclient
now you should be able to run these cells and get pretty-printed HTML output:
# %%
%load_ext sql
# %%
%sql mysql+mysqldb://<user>:<password>#localhost/<dataBase>
# %%
%%sql
SELECT *
FROM <table>;
import pymysql
import pandas as a
conn=pymysql.connect(host='localhost',port=int(3306),user='root',passwd='YOUR_PASSWORD',db='YOUR_DATABASENAME')
df=a.read_sql_query("SELECT * FROM 'YOUR_TABLENAME' ",conn)
print(df)
Yes, you can. You can use the MySQL Connector library. Simply install it using pip, and then you can use it to interact with your database. See the sample code below:
import mysql.connector
db = mysql.connector.connect(
host="localhost",
user="mamp",
passwd=""
)
print(db)
I successfully installed pyodbc module for python 2.7. However, when input the following code to connect to teradata,
import pyodbc
conn = pyodbc.connect('DRIVER={Teradata};DBCNAME=<tdwc>;UID=<UID>;PWD=<UID>;QUIETMODE=YES;')
I got the following error;
Traceback (most recent call last):
File "", line 1, in
pyodbc.connect('DRIVER={Teradata};DBCNAME=;UID=;PWD=;QUIETMODE=YES;')
Error: ('00000', '[00000] [iODBC][Driver Manager]dlopen(/Library/Application Support/teradata/client/ODBC/lib/tdata.dylib, 6): Library not loaded: libtdparse.dylib\n Referenced from: /Library/Application Support/teradata/client/ODBC/lib/tdata.dylib\n Reason: image not found (0) (SQLDriverConnect)')
What should I do to have this fixed? Any ideas?
Basically pyodbc is not straight forward and gives good enough debugging time for developers.
Follow the below steps,
You might have done them already, then just verify
Install iodbc for mac http://www.iodbc.org/dataspace/iodbc/wiki/iODBC/Downloads
Install Teradata ODBC Driver for Mac OS X, http://downloads.teradata.com/download/connectivity/teradata-odbc-driver-for-mac-os-x
Also install unixodbc for mac, “brew install unixodbc”
Download pyodbc source and change the setup.py file as below,
elif sys.platform == 'darwin':
# OS/X now ships with iODBC.
settings['libraries'].append('iodbc')
settings['libraries'].append('odbc')
you will be adding the last line of including “odbc” for build
Build and install pyodbc
Setup the traditional LD_LIBRARY_PATH for mac as below ( I used TD version 15.00, you have to point to the one as you installed)
export DYLD_LIBRARY_PATH=/Library/Application\
Support/teradata/client/ODBC/lib:/Library/Application\
Support/teradata/client/15.00/lib/
Export ODBCINI path
export ODBCINI=/Library/Application\
Support/teradata/client/15.00/odbc/odbc.ini
Now pyodbc cursor happily will fetch record sets for you
I found below link
http://python.projects.pgfoundry.org/docs/1.0/driver.html#connection-keywords
it says...
import postgresql.driver as pg_driver
is the way to import
i used -
import postgresql.driver as pg_driver
pg_driver.connect(user = self.username, password = self.password, host = self.host, port = self.port,sslmode = 'verify-full', sslrootcert=self.ssl_cert)
but it gives 'ImportError: No module named postgresql.driver'
i tried 'pip install postgresql'
but it gives 'Could not find any downloads that satisfy the requirement postgresql
No distributions at all found for postgresql'
how can i fixed that ???
You can download latest version of py-postgresql and use 'Python34\python.exe setup.py install' to install it.
You can check Python34\Lib\site-packages\ if there is a folder called postgresql which proves it's installed correctly. Then try import postgresql.driver as pg_driver again and tell me if it works.