Is there a module for python 3.3 to connect with Oracle Databases? Which is the easiest to use? Something like the mysql module, only works with Oracle.
Preferably version 10g, but 11g will do just fine.
There is: cx_Oracle
# Install --> You should have oracle installed otherwise exception will be raised
pip install cx_Oracle
import cx_Oracle
con = cx_Oracle.connect('pythonhol/welcome#127.0.0.1/orcl')
print con.version
con.close()
http://www.orafaq.com/wiki/Python
http://www.oracle.com/technetwork/articles/dsl/python-091105.html
if you're using python3
pip3 install cx_Oracle
How to connet oracle and get oracle time:
#!/usr/bin/python3
#coding=utf8
# import module
import cx_Oracle
con = cx_Oracle.connect('username/password#databasename')
# create cursor
cursor = con.cursor()
# execute sql
cursor.execute('select sysdate from dual')
# fetch one data, or fetchall()
data = cursor.fetchone()
print('Database time:%s' % data)
# close cursor and oracle
cursor.close()
con.close()
Related
I installed pyodbc with pip a couple of weeks ago, all my other scripts work and connect to SQL Server perfectly fine but for some reason this script won't run... I've uninstalled and reinstalled it still giving same error
python code
import pyodbc
connector = pyodbc.connect("DRIVER={ODBC Driver 18 for SQL Server};SERVER=localhost;UID=SA;PWD=Working#2022;DATABASE=testdb;Encrypt=no;TrustServerCertificate=yes")
cursor = connector.cursor()
cursor.execute("USE testdb")
cursor.execute("SELECT * FROM overtime_forecast")
for i in cursor:
print(i)
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 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!!!
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)