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
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
Windows: Windows 7 Professional
Python: python-3.6.1-amd64.exe
pyodbc: pyodbc-4.0.16-cp36-cp36m-win_amd64.whl
Eclipse: Neon.3Release(4.6.3) Build id: 20170314-1500
PyDev for Eclipse: 5.7.0201704111357
After I installed pyodbc by run pip install pyodbc-4.0.16-cp36-cp36m-win_amd64.whl, I got unresolved import pyodbc in Eclipse. So I manually added "pyodbc" under Python Interpreter > Forced Builtins and "unresolved import pyodbc" issue solved in Eclipse.
However, auto completion does not work for variable in Eclipse with Pydev. I can get auto completion for pyodbc.connect but not conn.cursor() unless I define conn = pyodbc.Connection.
Auto completion not working for variable conn
import pyodbc
if __name__ == '__main__':
conn = pyodbc.connect('Connecting String....')
cur = conn.Cursor()
Auto completion works for variable conn
import pyodbc
if __name__ == '__main__':
conn = pyodbc.Connection
conn = pyodbc.connect('Connecting String....')
cur = conn.Cursor()
This is mostly because PyDev can't infer what pyodbc.connect returns (it can't really execute that, it can only see that it's a method call and try to guess from its docstring).
You can help it though by adding type hints in docstrings as:
#: :type conn: pyodbc.Connection
See: http://www.pydev.org/manual_adv_type_hints.html for more info
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()
This question already has answers here:
Python module "cx_Oracle" module could not be found
(4 answers)
Closed 4 years ago.
Im using python 2.7 and cx_oracle ( Windows x86 Installer (Oracle 10g, Python 2.7) ) and 'm having a bad time to set this simple example bellow to work:
import cx_Oracle
connection = cx_Oracle.connect('user/pass#someserver:port')
cursor = connection.cursor()
cursor.execute('select sysdate from dual')
for row in cursor:
print row
connection.close()
Error Message:
Traceback (most recent call last):
File "C:\ORACON.py", line 1, in <module>
import cx_Oracle
ImportError: DLL load failed: The specified module could not be found.
For now, what i have done was:
1) installed the cx_oracle binary;
2) downloaded instantclient_10_2 from oracle website and exported the path to environment;
Anyone know what im missing?
Thank you for your time on reading this.
I was able to solve this problem with the following steps:
Download instantclient-basic-win32-10.2.0.5 from Oracle Website
unzipped the into my c:\ with the name oraclient
Created the directory structure C:\oraclient\network\admin to add the TNSNAMES.ORA
Added the TNS_ADMIN env var pointing to C:\oraclient\network\admin
Added the ORACLE_HOME env var pointing to C:\oraclient\
After that i used the following code:
import cx_Oracle
con = cx_Oracle.connect('theuser', 'thepass', 'your DB alias on your TNSNAMES.ORA file ')
cur = con.cursor()
if cur.execute('select * from dual'):
print "finally, it works!!!"
else:
print "facepalm"
con.close()
I hope it helps someone.