Install ODBC on Ubuntu 20.04, 20.10, and above
There used to easy apt install for ODBC like “apt-get install libmyodbc” , not anymore. You have to do some extra effort now
Follow these steps.
apt install unixodbcapt unixodbc-dev -y
cd ~
tar -xvf mysql-connector-odbc-5.3.6-linux-ubuntu15.10-x86–64bit.tar.gz
sudo cp mysql-connector-odbc-5.3.6-linux-ubuntu15.10-x86–64bit/lib/libmyodbc5* /usr/lib/x86_64-linux-gnu/odbc/
sudo mysql-connector-odbc-5.3.6-linux-ubuntu15.10-x86–64bit/bin/myodbc-installer -d -a -n “MySQL” -t “DRIVER=/usr/lib/x86_64-linux-gnu/odbc/libmyodbc5w.so;”
sudo mysql-connector-odbc-5.3.6-linux-ubuntu15.10-x86–64bit/bin/myodbc-installer -s -a -c2 -n “test” -t “DRIVER=MySQL;SERVER=127.0.0.1;DATABASE=mysql;UID=root;PWD=123456”
sudo mysql-connector-odbc-5.3.6-linux-ubuntu15.10-x86–64bit/bin/myodbc-installer -s -a -c2 -n “test” -t “DRIVER=MySQL;SERVER=127.0.0.1;DATABASE=mysql;UID=root;PWD=123456”
Following steps are for testing the connectivity
odbcinst -q -d
odbcinst -q -s
isql test — will also give you connected message, make sure that your have correct DB and table otherwise it will throw “[ISQL]ERROR: Could not SQLConnect”.
Create a test python script to test connectivity
apt install python3-pip
pip install — upgrade pyodbc
vim test.py
add following lines in test.py
— — — — — — — — — — — — — — — — —
# coding=UTF-8
import pyodbc
print(“connecting …”)
cnxn = pyodbc.connect(“DSN=test”)
cursor = cnxn.cursor()
rows = cursor.execute(“select * from TABLES”).fetchall()
cursor.close()
except Exception as e:
print(repr(e))
— — — — — — — — — — — — — -
chmod 777 test.py
python3 test.py
You will see it connected !!!
Following are the sample files, in case you need troubleshooting.
Originally published at https://erpgulf.com on January 29, 2021.