How to connect to a SQL Server database in Anaconda - python

I'm quite new to Python and programming in general, so please bear with me. At my work I have Anaconda (Python vers 2.7) installed and would like to connect to a Microsoft SQL Server database, preferably with ODBC. Related to this task I have a number of questions:
Is it correct that I cannot connect to a SQL Server database using sqlite3 or sqlalchemy? That I need a module like pyodbc? A brief explanation of why this is the case would be much appreciated.
EDIT: Question related to installation of pyodbc in anaconda removed, since I figured this out (by reading Cannot Install Python Modules after Installing Anaconda)
Help is much appreciated! If any of my questions/any other specifics need to be cleared up, please don't hesitate to ask. Thanks

I do not use Anaconda, but I use various databases and ODBC. At first you can try if you have odbc module installed. It is a part of pywin32 package (http://sourceforge.net/projects/pywin32/files/) and is packed with ActiveState Python distribution. Other distribution can install it separately. Simply try:
import odbc
db = odbc.odbc('dsn/user/password')
You can also try with pyodbc you mentioned in question. There is precompiled version for Windows and I think it will work with your Anaconda environment. After installing try:
import pyodbc
db = pyodbc.connect('Driver={SQL Server Native Client 10.0};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;')
You can find more connection strings at http://www.connectionstrings.com/
EDIT:
It seems that you have problem with bitness of ODBC driver.
Try to run this program to see what sources are visible to ODBC manager:
import odbc
source = odbc.SQLDataSources(odbc.SQL_FETCH_FIRST)
while source:
print(source)
source = odbc.SQLDataSources(odbc.SQL_FETCH_NEXT)

Related

Can I query Microsoft SQL Server using pure Python?

I need to query SQL Server from Python in a machine that I don't have admin rights. I can't install drivers in it. I'll query mainly from a Linux machine, but it would be nice if I it can also be used from Windows during development.
I want to make a simple query in MS SQL Server with a Python lib. A lib that doesn't need any driver installed in the Operating System. Is it possible? Everything that I found asks to install the Microsoft driver in the host OS.
A quick search turns up pydts but it's not supported by Microsoft and I have no idea how widely adopted it is or if it's being actively maintained. Here's the GitHub repo.

How do I connect to Oracle Exadata from Python?

I have tried connecting exadata from python using cx_Oracle, but faced issue stating
cx_Oracle.DatabaseError: DPI - 1047: Cannot locate a 64bit Oracle client library.
I am trying to install a 64bit cx_Oracle, but I need to know can I connect from python to exadata.
Follow the cx_Oracle Linux installation instructions. The piece you are missing is access to Oracle Client libraries. As noted in the other answer, using Instant Client is the way to go. You'll need the "Basic" or "Basic Light" package for Linux x86-64. If you don't know your character set requirement, then use "Basic". You can use the latest 19c version. This will let you connect to Oracle DB 11.2 and newer.
If you have root access (?) you may find the RPM packages easier. If you want to use ZIP files, unzip the package, set your LD_LIBRARY_PATH environment variable to the directory, and run Python.
In your cx_Oracle scripts you use the same database credentials and connection string that you would use in SQL*Plus.
Welcome to Stack Overflow!
For the record, Oracle Exadata is a platform consisting of hardware and software designed for extreme Oracle Database performance. cx_Oracle is the Python client library to operate on an Oracle Database - hence you want to connect to an Oracle Database utilizing the Python programming language.
cx_Oracle depends on OCI (Oracle Call Interface). You need to download and install Oracle Client Library on your workstation before you can do import cx_Oracle from within Python.
You will find the Oracle Client Library here
Best of luck!

How to connect to remote MS SQL Server from python using py(py)odbc

I have a remote ms sql server at office location and I wanted to connect from an ubuntu desktop to it. I installed DBeaver http://dbeaver.jkiss.org/ and it was working like a charm without any other dependency.
After this I started to setup a connection from python with no success until I found a straightforward tutorial at https://tryolabs.com/blog/2012/06/25/connecting-sql-server-database-python-under-ubuntu/. But this solution involved installing some packages to ubuntu itself and configuring some related config files in the system.
The question is if there is some python package to use without os dependencies?
I don't think you would be able to achieve what you want without installing some packages. Reason being you are connecting to the MSSQL which follows a different set of rules as it is from Microsoft.
If you were connecting to any DB (MySQL,MongoDB etc) you still need to install a package or module to make it work.
Try to install the package as described in the tutorial from the like you shared and if you run into any problem paste the problem here.

pypyodbc: [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionWrite (send())

My python script reads through around half of the dataset (around 5000 rows out of 10000) and all of a sudden produces this error midway:
[Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionWrite (send())
I'm using pypyodbc to connect to the Azure SQL server. I haven't been able to find the solution anywhere online. I don't even know whats the cause that's leading to this error.
Please help.
Hope you are doing well. Have you had a chance to look at pyodbc and pymssql?
They have a better community presence and are actively maintained. If the script fails with these drivers as well, there might be an issue with your script. I can try to help you troubleshoot if you share it.
Let me know if the issue gets fixed with the other drivers and if it does not I can help you out.
Thanks,
Meet Bhagdev
Program Manager, Microsoft
I tried to reproduce the issue, but failed. And I observed that the package last updated in 2013. It's an old and no longer maintenance package for Python ODBC. So I don't recommend to continue to use it to access the latest Azure SQL Database.
I strongly recommended you to use the package pymssql. It's the Azure offical recommend odbc driver for Python, please refer to https://azure.microsoft.com/en-us/documentation/articles/sql-database-libraries/.
It can very good support any OS environment contains Windows, Linux, MacOS. You can refer to these docs https://azure.microsoft.com/en-us/documentation/articles/sql-database-develop-python-simple-windows/, https://azure.microsoft.com/en-us/documentation/articles/sql-database-develop-python-simple-ubuntu-linux/ and https://azure.microsoft.com/en-us/documentation/articles/sql-database-develop-python-simple-mac-osx/ to getting started.
If you want to dive into the package usgae, please refer to the offical document http://pymssql.org/en/latest/index.html.
Best Regards.

How do I access an Oracle db without installing Oracle's client and cx_Oracle?

I have two RHEL servers running Python 2.4 and 2.6 separately. There is an Oracle database on the other server I need to access.
I was trying to install cx_oracle on my RHEL server but found out that the Oracle client must be installed first.
The problem is, I don’t have permission to install Oracle's client on both RHEL servers. On the same servers, a Perl program can connect to the Oracle db using:
DBI->connect("dbi:Oracle:host=myhost.prod.com;sid=prddb",'username','password')
Can Python do the same without installing cx_oracle and the Oracle client? Or are there any suggestions about how to write a module myself to do the same thing?
Thanks in advance!
An excerpt from https://forum.omz-software.com/topic/184/oracle-database:
There's no pure python client for Oracle and likely never will be. Even wonderful third-party toolsets like SQLalchemy still rely on cx_Oracle underneath to do the actual communication to the Oracle database servers.
—also, deciding by Google, the answer is no: there do not seem to be any pure Python Oracle clients in existence as of today.
Usually, all you need are the libraries, which don't necessarily require sudo rights. Extract them to a place the software will be able to read from and set the following environment variables accordingly:
ORACLE_HOME=path/to/where/you/extracted/libs
TNS_ADMIN=path/to/tnsnames.ora
I have had best luck skipping tnsnames, and just specifying the host, port, etc in the connection, but it's quite possible you'll need it for cx_oracle...I don't remember from when I used it ages ago.
if you don't want use cx_Oracle you should use expect scripting. ( for python pexpect). But you need to be carefully for handle all expectations.

Categories