python connecting to Oracle using pyodbc sometimes kills python interpreter - python

Periodically when using pyodbc to create a connection to an oracle database, it kills the interpreter.
import pyodbc
connectString = 'Driver={Microdsoft ODBC for Oracle};Server=<host>:<port>/<db>.<host>;uid=<username>;pwd=<password>'
cnxn = pyodbc.connect(connectString)
Say 1/5 of the time this will just drop me from the Python prompt (>>>) down to my dos prompt (C:)
Any ideas of why this happens or how to fix it?

We could not discover what was exactly causing this problem. Research leads us to think that there may be a problem with Python 2.7.1, PyODBC, and Win 2003 server.
To fix this problem we created a jar file that executed the permission check and then had the python script call that file and return the result

Related

DB Connect failure python cx_Oracle - ORA-01804

I try to connect oracle DB with cx_Oracle package in python 3.9.7.
cx_Oracle version is 8.3.0.
when i try to connect with command cx_Oracle.clientversion() i got error :
DatabaseError: Error while trying to retrieve text for error ORA-01804
Linux Os , attach my .bash_profile
with sqlplus command i success to connect the DB.
.bash_profile :
Thanks
This error generally occurs when there is a discrepancy between the value of the environment variable ORACLE_HOME and the actual library that was loaded. You can set the environment variable DPI_DEBUG_LEVEL to the value 64 and run your script. It will tell you which method was used to load the library. If that doesn't help you figure it out, paste the output in your question and I'll try to help further.
Note as well that there is a new driver available (python-oracledb) which doesn't require Oracle Client libraries and therefore shouldn't run into this issue. Take a look here: https://levelup.gitconnected.com/open-source-python-thin-driver-for-oracle-database-e82aac7ecf5a

Windows Python - invoking SQL Server stored procedure works only in Python IDLE

Windows 10 Pro, Python 3.9, SQL Server 2012.
I have written a bunch of Windows-based Python scripts over the years. One aspect, I use the pypyodbc library to insert data harvested via Python into SQL Server. This has been working well for years whether my Python script is run from the Python IDLE or if the Python script is run from the command line via a Windows batch (.bat) file for automation. 
I recently introduced calling a, fully operational, SQL Server stored procedure from my Windows-based Python script that is already doing SQL Server inserts successfully. When I run my Python script from the Python IDLE, the SQL Server inserts and the stored procedure runs successfully.
PROBLEM: when I run my Python script via a Windows batch (.bat) file, the SQL Server inserts are successful but the stored procedure does not run. I have also tried "Run as administrator" for the .bat file invoking my Python script. Still, the stored procedure will not run.
Given the SQL Server inserts are successful no matter what and the stored procedure WILL ONLY run when originating the Python script via the Python IDLE, perhaps this problem is a Windows configuration issue?
Thanks in advance for any guidance.
I located the problem. It was a permissions problem in SQL Server. This is now closed. Thanks.

Is it possible to use SQL Server in python without external libs?

I'm developing on an environment which I'm not allowed to install anything. It's a monitoring server and I'm making a script to work with logs and etc.
So, I need to connect to a SQL Server with Python 2.7 without any lib like pyodbc installed. Is it possible to make this? I've found nothing I could use to connect to that database.
There are certain things you can do to run sql from the command line from python:
import subprocess
x = subprocess.check_output('sqlcmd -Q "SELECT * FROM db.table"')
print x

Install MYSQLdb python module without MYSQL local install

I'm trying to install MYSQLdb on a windows client. The goal is, from the Windows client, run a python script that connects to a MySQL server on a LINUX client. Looking at the setup code (and based on the errors I am getting when I try to run setup.py for mysqldb, it appears that I have to have my own version of MySQL on the windows box. Is there a way (perhaps another module) that will let me accomplish this? I need to have people on multiple boxes run a script that will interact with a MySQL database on a central server.
you could use a pure python implementation of the mysql client like
pymysql
(can be used as a dropin-replacement for MySQLdb by calling pymysql.install_as_MySQLdb())
MySql-Connector
You don't need the entire MySQL database server, only the MySQL client libraries.
It's been a long time since I wrote python db code for windows...but I think something like this should still work.
If you're running the client only on windows machines, install the pywin32 package. This should have an odbc module in it.
Using the windows control / management tools, create an odbc entry for either the user or the system. In that entry, you'll give the connection parameter set a unique name, then select the driver (in this case MySQL), and populate the connection parameters (e.g. host name, etc.) See PyWin32 Documentation for some notes on the odbc module in pywin32.
Also, see this post: Common ways to connect to odbc from python on windows.

pyodbc and mySQL

I am unable to connect to mySQl db using pyodbc.
Here is a snippet of my script:
import pyodbc
import csv
cnxn = pyodbc.connect("DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost;DATABASE=mydb; UID=root; PASSWORD=thatwouldbetelling;")
crsr = cnxn.cursor()
with open('C:\\skunkworks\\archive\\data\\myfile.csv','r') as myfile:
rows = csv.reader(myfile, delimiter=',', quotechar='"')
for row in rows:
insert_str = 'INSERT into raw_data VALUES(something, something)'
print insert_str
#crsr.execute(insert_str)
cnxn.commit()
myfile.close()
I get this error at the pyodbc.connect() line:
pyodbc.Error: ('IM002', '[IM002]
[Microsoft][ODBC Driver Manager] Data
source name not found and no default
driver specified (0)
(SQLDriverConnectW)')
I have another question regarding this error (and Python scripts in general). When I run this as a script, it fails silently (I was expecting a stack trace). I have to type each line in manually to find where the error occured.
I am being a bit lazy for now (no exception handling) - is this normal behaviour of a Python script without exception handling to fail silently?
[Edit]
I am not using mysqldb because I am already using pyodbc to extract my data from another source (MS Access). Ok, not a good reason - but I am already grappling with pyodbc and I dont really fancy having to wrestle with another library/module/package(whatever its called in Python) for a "one off" job. I just want to move my data of from various data sources in the Windows environment to mySQl on Linux. once on Linux, I'll be back on terra firma.
That is the entire 'script' right there. I just saved the code above into a file with a .py extension, and I run python myscript.py at the command line. I am running this on my XP machine
I had this same mistake so I went over all the version I was using for the connection. This is what I found out:
For Python 2.7 32 bits:
- pyodbc must be 32bits
- the DB Driver must be 32bits. (Microsoft Access should be 32 bits too)
For those who use the 64 bits version. You should check that everything is 64 bits too.
In my case I was trying to connecto to an Oracle DB and Microsoft Access DB so I had to make the following components match the architechture version:
pyodbc (MS Access)
python
cx_Oracle (Oracle dialect for SQLalchemy)
Oracle instantclient basic (Oracle. Do not forget to create the environment variable)
py2exe (Making the excecutable app)
Is that your driver name right?
You can check your driver name in
Windows -> Control panel -> System and security -> Administrative tools -> ODBC Data Sources -> Driver tab
then copy the river name to the first parameter
like
cnxn = pyodbc.connect("DRIVER={MySQL ODBC 5.3 ANSI Driver}; SERVER=localhost;DATABASE=books; UID=root; PASSWORD=password;")
And my problem solved
or you may not install the driver and the step is simple.
MySQLdb (or oursql) and pyodbc both have the same interface (DB-API 2), only you don't have to deal with ODBC's issues if you use the former. I strongly recommend you consider using MySQLdb (or oursql) instead.
First, According to the official docs, if you want to connect without creating a DSN, you need to specify OPTION=3 in the connection string:
ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=test;USER=venu;PASSWORD=venu;OPTION=3;"
If that fails to work, I'd further troubleshoot by creating a DSN.
Second, no Python should not be failing silently. If that is the case when you run your script, there is something else amiss.
only need install mysql-connector-odbc-3.51.28-win32.msi.
and pyodbc-2.1.7.win32-py2.7.exe.
of course, you have ready installed MySQL and python 2.7.
example:
import pyodbc
cndBase = pyodbc.connect("DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; PORT=3306;DATABASE=nameDBase; UID=root; PASSWORD=12345;")
ptdBase = cndBase.cursor()
query_str = 'SELECT* FROM nameTabla;'
rows = ptdBase.execute(query_str)
for rw in rows:
print list(rw)`enter code here`
I was getting the same error. It seemed the driver i was using to make the connection was not the driver installed in my system.
Type ODBC on windows run and select ODBC Data Source(32/64) based on where you have installed the driver.
From there click on System DSN and click add. From there you can see the MySQL driver installed in your system. Use the ANSI driver in your code where you are making the connection.

Categories

Resources