All the examples I found either mysql or sqlite.
Connect base:
connect_base = adbapi.ConnectionPool("mssql", user="test", password="test", host="192.168.0.1", database="test")
I get an error:
ImportError: No module named mssql
Install pymssql
pip install pymssql
and use:
connect_base = adbapi.ConnectionPool("pymssql", user="test", password="test", host="192.168.0.1", database="test")
Related
I followed this website by installing
Install:
sudo apt-get install freetds-dev freetds-bin unixodbc-dev tdsodbc
pip install pyodbc sqlalchemy
In /etc/odbcinst.ini:
[FreeTDS]
Description=FreeTDS Driver
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup=/usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
After install I try this code and it worked, it print all records from table2
import pyodbc
conn = pyodbc.connect('DRIVER=FreeTDS;SERVER=<IP_OR_HOSTNAME>;PORT=1433;DATABASE=<DATABASE_NAME>;UID=<USERNAME>;PWD=<PASSWORD>;TDS_Version=8.0;')
cursor = conn.cursor()
for row in cursor.execute('SELECT * FROM Table2'):
print (row)
However, I want to use SQLAlchemy with pyodbc and it does not work
import urllib
from sqlalchemy import create_engine
engine = create_engine('mssql+pyodbc:///?odbc_connect=' +
urllib.parse.quote_plus('DRIVER=FreeTDS;SERVER=<IP_OR_HOSTNAME>;PORT=1433;DATABASE=<DATABASE_NAME>;UID=<USERNAME>;PWD=<PASSWORD>;TDS_Version=8.0;')
)
for row in engine.execute('SELECT * FROM Table2'):
print (row)
ProgrammingError: (pyodbc.ProgrammingError) ('42000', "[42000] [FreeTDS][SQL Server]'schema_name' is not a recognized function name. (195) (SQLExecDirectW)")
[SQL: SELECT schema_name()]
(Background on this error at: https://sqlalche.me/e/14/f405)
How can I connect this?
SQLAlchemy 1.4 does not support SQL Server 2000. Use pip install sqlalchemy==1.3.24.
I followed this website by installing
Install:
sudo apt-get install freetds-dev freetds-bin unixodbc-dev tdsodbc
pip install pyodbc sqlalchemy
In /etc/odbcinst.ini:
[FreeTDS]
Description=FreeTDS Driver
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup=/usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
After install I try this code and it worked, it print all records from table2
import pyodbc
conn = pyodbc.connect('DRIVER=FreeTDS;SERVER=<IP_OR_HOSTNAME>;PORT=1433;DATABASE=<DATABASE_NAME>;UID=<USERNAME>;PWD=<PASSWORD>;TDS_Version=8.0;')
cursor = conn.cursor()
for row in cursor.execute('SELECT * FROM Table2'):
print (row)
However, I want to use SQLAlchemy with pyodbc and it does not work
import urllib
from sqlalchemy import create_engine
engine = create_engine('mssql+pyodbc:///?odbc_connect=' +
urllib.parse.quote_plus('DRIVER=FreeTDS;SERVER=<IP_OR_HOSTNAME>;PORT=1433;DATABASE=<DATABASE_NAME>;UID=<USERNAME>;PWD=<PASSWORD>;TDS_Version=8.0;')
)
for row in engine.execute('SELECT * FROM Table2'):
print (row)
ProgrammingError: (pyodbc.ProgrammingError) ('42000', "[42000] [FreeTDS][SQL Server]'schema_name' is not a recognized function name. (195) (SQLExecDirectW)")
[SQL: SELECT schema_name()]
(Background on this error at: https://sqlalche.me/e/14/f405)
How can I connect this?
SQLAlchemy 1.4 does not support SQL Server 2000. Use pip install sqlalchemy==1.3.24.
I am using a macOs BigSur, and I would like to use python to connect to azure sql database.
I followed Microsoft documentation:
https://learn.microsoft.com/en-us/python/api/overview/azure/sql?view=azure-python
to set all the configuration and installed all the requirement for the Mac OS following this documentation.
https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver15
but when I run my python script I get this error:
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")
I install mssql-tools and unixodbc
and if I run the command odbcinst -j
I get back this output:
unixODBC 2.3.9
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /Users/<my-user>/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
Anyone has a clue about this error? and please if you need more infos just ask me. Thank you so much
EDIT:
Following the documentation, Microsoft suggests to make a sim link out of the odic.ini and odbcinst.ini.
I run the code to create those symlinks but I realised that if I try to open those files, they are empty, and if I try to go to the folder and open them manually rather than with terminal, I get the error that it can't be done because the source doesn't exist.
Did anyone ever occurred in this error?
LATEST UPDATE:
I installed FreeTDS and updated my .odbc.ini with the following configuration:
[my_server]
Description = my_server
TDS_Version = 7.4
Driver = /usr/local/lib/libtdsodbc.so
Server = YOUR.SERVERNAME.HERE.com
Port = 1433
When I run my python script to connect to the database, I get the following Traceback:
Traceback (most recent call last):
File "database.py", line 11, in <module>
cnxn = pyodbc.connect('dsn=my_server;'
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib '/usr/local/lib/libtdsodbc.so' : file not found (0) (SQLDriverConnect)")
And in fact there is no libtdsodbc.so in that location.
In another topic they suggest to install freeTDS with the flag --with-unixodbc but when I try to do so, I get the error that the command doesn't exist
UPDATE
If your macos is on the x64 architecture, you can use below method to solve issues.
If you use M1, it's not support now.
Solution for x64 architecture.
Run below code, it should useful to you.
brew update
brew install unixodbc
brew install FreeTDS
Then update the .odbc.ini file.
For more details, please refer below blogs.
How to set up ODBC in Mac OS to connect to MS SQL Server for use with Python and R
Starting ODBC version 17.8, Apple M1 is supported.
You can install ODBC 18 on macOS with the following commands:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
HOMEBREW_NO_ENV_FILTERING=1 ACCEPT_EULA=Y brew install msodbcsql18 mssql-tools18
I can connect to HIVE using sqlalchemy without any issues :
oguz#dikanka:~$ python
Python 2.7.15+ (default, Jul 9 2019, 16:51:35)
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sqlalchemy import create_engine
>>> engine = create_engine('hive://dikanka:10000')
>>> result = engine.execute("select movieid from default.ratings")
>>> result.first();
(242,)
But when I try to connect Superset to Hive, I receive the following error :
ERROR: {"error": "Connection failed!\n\nThe error message returned was:\nCan't load plugin: sqlalchemy.dialects:hive"}
Details are as follows :
I have a single-node HDP-3.1.4.0 setup on my laptop.
I have the following services up and running : HDFS 3.1.1, Hive 3.1.0, Zookeeper 3.4.6, Superset 0.23.0
Python version is Python 2.7.15+.
Hive command line interface works smoothly.
I've installed the following python modules:
pip install thrift
sudo apt-get install libsasl2-dev
pip install sasl
pip install thrift_sasl
pip install pyhive
Last but not least, I have a KUbuntu 18.04.
On Superset, I try to add a database named "default", and I set the SQLAlchemy URI to
hive://dikanka:10000
which is exactly the same URI I used with create_engine above.
Then I click "Test Connection", with all default values for the rest of the form.
I get the error as mentioned above.
In order to instal the hive sqlalchemy driver you need to do
pip install 'pyhive[hive]'
That should fix the error you got.
Here is my code:
import os
import pandas as pd
def load_hdf(filename):
"""
Load the first key of an HDF file
"""
hdf = pd.HDFStore(filename,mode = 'r')
keys = hdf.keys()
if not keys:
hdf.close()
return pd.DataFrame()
data_df = hdf.get(keys[0])
hdf.close()
return data_df
And when I do:
load_hdf(os.path.join(PATH, 'crm.hd5'))
I have this error:
HDFStore requires PyTables, "No module named 'tables'" problem importing
When I try:
pip install tables
I have the error:
Using Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
* USE_PKGCONFIG: False
.. ERROR:: Could not find a local HDF5 installation.
You may need to explicitly state where your local HDF5 headers and library can be found by setting the ``HDF5_DIR`` environment variable or by using the ``--hdf5`` command-line option.
...
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/2s/sn3gzfwd6_37v0ggqd0n8qy00000gn/T/pip-install-1mx6wjd3/tables/
I already have Pytables, hdf5 in my Anaconda. I have Python 3.7.
I also had pytables installed and could not find a solution. What worked for me is to install the release candidate of HDF5 2.8.0rc1 (as seen here). Seems that the HDF5 version that panda installs is not fully compatible.
So try:
pip install h5py==2.8.0rc1
Hope it helps.