isql works but python connection fails - python

I'm trying to hook up odbc to work on my Mac with python via pyodbc. I've got things set up such that
isql -v <my Connection>
works just file, however when I'm in a python shell and I try:
import pyodbc
pyodbc.connect('DSN=<my Connection>;UID=<username>;PWD=<password>
I get the cryptic:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pyodbc.Error: ('HY000', '[] (202) (SQLDriverConnect)')
What can I do to debug this and determine what steps I can take next to get odbc working? Previously, I had odbc installed and was getting "no driver source name" messages in python, and after moving the odbc.ini and odbcinst.ini file to /etc, I now get the error above. Any ideas? Thanks for your time.

Are you trying to connect to SQL Server? Give this a try:
DRIVER={FreeTDS};SERVER=<yourserver.example.com>;PORT=1433;DATABASE=<your DB>;UID=<your username>;<your password>;TDS_Version=7.2;
Hopefully, that'll work for you. The TDS version depends on what version of SQL Server you're using. See here:
http://www.freetds.org/userguide/choosingtdsprotocol.htm
7.2 also works for 2012 + 2014.
Regards,
-Tim

Related

Python mariadb module does not connect to database on network

I am trying to connect to a mariadb-database on my local network. using Python.
import mariadb
cursor = mariadb.connect(host='192.168.178.77', user='someuser', password='somepass', db='temps')
Output is:
Traceback (most recent call last):
File "/Users/localuser/PycharmProjects/SQL/main.py", line 20, in <module>
cursor = mariadb.connect(host='192.168.178.77', user='someuser', password='somepass', db='temps')
File "/Users/user/.conda/envs/SQL/lib/python3.10/site-packages/mariadb/__init__.py", line 142, in connect
connection = connectionclass(*args, **kwargs)
File "/Users/localuser/.conda/envs/SQL/lib/python3.10/site-packages/mariadb/connections.py", line 86, in __init__
super().__init__(*args, **kwargs)
mariadb.OperationalError: Can't connect to server on '192.168.178.77' (60)
I can connect via Pycharms Database functionality and send SQL Statements.
I also can use DB management tools from that very host and use data without any issue.
It even works from my phone.
This code is the only place where I get an error.
OS is MacOS13.0.1
Thank You!
This happens due to a bug in MariaDB Connector/C. (Issue CONC-612).
The issue was fixed in C/C Version 3.3.3 - which is available via brew:
After
brew update
brew upgrade mariadb-connector-c
connection should work as expected.
I've got the same problem recently. Add port variable and check other. If doesn't help, try mysql-connector-python it works similar. Or install mariadb connector manually

Instal postgres sql in windows 10

I try to install the latest version of postgres sql using the latest version and the recommended set up.
However when I open the pg adming I receive this error.
Python Path: "C:\Program Files\PostgreSQL\13\pgAdmin 4\python\python.exe"
Runtime Config File: "C:\Users\User\AppData\Roaming\pgadmin\runtime_config.json"
pgAdmin Config File: "C:\Program Files\PostgreSQL\13\pgAdmin 4\web\config.py"
Webapp Path: "C:\Program Files\PostgreSQL\13\pgAdmin 4\web\pgAdmin4.py"
pgAdmin Command: "C:\Program Files\PostgreSQL\13\pgAdmin 4\python\python.exe C:\Program Files\PostgreSQL\13\pgAdmin 4\web\pgAdmin4.py"
Traceback (most recent call last):
File "../web/pgAdmin4.py", line 98, in <module>
app = create_app()
File "C:\Program Files\PostgreSQL\13\pgAdmin 4\web\pgadmin\__init__.py", line 347, in create_app
if not os.path.exists(SQLITE_PATH) or get_version() == -1:
File "C:\Program Files\PostgreSQL\13\pgAdmin 4\web\pgadmin\setup\db_version.py", line 19, in get_version
return version.value
AttributeError: 'NoneType' object has no attribute 'value'
Any help on how to fix it?
After trying all the other suggestions without success, it finally worked for me now, with the way suggested here: Get fatal error: server could not be contacted when running pgAdmin4. 'NoneType' object has no attribute 'value'
Install the newest version of the standalone pgAdmin from https://www.pgadmin.org/download/pgadmin-4-windows/
(you can do the installation additionally to the existing bundled version, without having to remove anything before)
Delete the C:\Users\your_username\AppData\Roaming\pgadmin folder
(I restarted the postgres service once again)
Run the new pgAdmin as Administrator - it works!
Several people report that, from then on, even the old bundled version of pgAdmin works fine!
I resolved in this way:
I removed PGadmin from "Application Stack Builder"
download last version v4 5.2 (pgAdmin 4 v5.2 (released April 22, 2021)
installed...
lunched like admin... (error as usual)
clean-up folder C:\Users\XXXXX\AppData\Roaming\pgadmin (opsssss)
lunched like admin... All ran successfully!
Using all in one PGadmin with Application Stack gave me an usual error bind to some pyton or configuration file
Alen
I had a lot of trouble launching pgAdmin. Several solutions are there.
Try an older version of pgAdmin. The latest version might have unresolved issues.
Go to %APPDATA% (C:\Users{{your username}}\AppData\Roaming) folder and delete the pgadmin folder and run again.
go to environment variables (search environment on search bar) -> advanced, the click on Environment Variables. On System Variables click on path and set a new path for C:\Program Files\PostgreSQL\13\bin (the address might vary on your PC)

Mysql-Python Connector issues on different approaches

In the process of downloading mysql connector, I've taken 3 approaches:-
The commonly advised pip approach where I think the PATH (Python on system; which I don't think I understand) fails me since it doesn't seem to install mysql.connector. It gives a deprecation, collects mysql.connector and launches an error.
Direct download through this site: https://dev.mysql.com/downloads/connector/python/ whereby I've changed security preferences(since it was an unidentified by apple developer) and tried downloading but the application doesn't appear anywhere on my desktop and I can't seem to find it anywhere else.
I followed this link: https://www.youtube.com/watch?v=1ji8lqiBJe0 and everything went fine till 1:34. I don't use pycharm so I decided to directly write import mysql.connector on idle.
At first, it would give me this error:-
Traceback (most recent call last):
File "", line 1, in
import mysql.connector
ModuleNotFoundError: No module named 'mysql
Now it presents this error:-
Traceback (most recent call last):
File "", line 1, in
import mysql.connector
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/init.py", line 41, in
import dns.resolver
ModuleNotFoundError: No module named 'dns'
I can't seem to understand where 'dns module' comes from. Could anybody help me on how to proceed? I think I've exhausted every method at this point but can't find an error.
Have you correctly install dnspython ?
pip install dnspython
https://github.com/rthalley/dnspython
and the connector for your OS
https://dev.mysql.com/downloads/connector/python/

How to extract a columns content in Microsoft Access Database using python

I am just starting to learn python and was wondering if someone could help me pull column data using python? I have been researching for about 3 hours.
code:
import pyodbc
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb,
*.accdb)};DBQ=C:\Users\jgp22\Documents\Database11.accdb;')
cursor = conn.cursor()
cursor.execute('select * from information')
for row in cursor.fetchall():
print(row)
error:
Traceback (most recent call last):
File "C:\Users\jgp22\Desktop\Python\Data.py", line 3, in <module>
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb,
*.accdb)};DBQ=C:\Users\jgp22\Documents\Database11.accdb;')
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager]
Data source name not found and no default driver specified (0)
(SQLDriverConnect)')
error #2:
Traceback (most recent call last):
File "C:\Users\jgp22\Desktop\Python\GrabData.py", line 3, in <module>
conn = pyodbc.connect(r'DSN=MAD')
pyodbc.Error: ('HY000', "[HY000] [Microsoft][ODBC Microsoft Access Driver]
Cannot open database '(unknown)'. It may not be a database that your
application recognizes, or the file may be corrupt. (-1028)
(SQLDriverConnect); [HY000] [Microsoft][ODBC Microsoft Access Driver] Cannot
open database '(unknown)'. It may not be a database that your application
recognizes, or the file may be corrupt. (-1028)")
Create a Named System DSN
Go to ODBC Admin (make sure its the 64-bit one)
Click on System DSN
Create named DSN that points to 'C:\Users\jgp22\Documents\Database11.accdb' in this example lets use myDsn
Use the connection string DSN=myDsn where myDsn is the name you gave in step 3. Don't worry about the driver info or path. All of that is stored with the DSN record on the system.
ODBC Data Source Administrator Paths
To setup DSN for 32-bit application you must use:
%WINDIR%\SysWOW64\odbcad32.exe
and for 64-bit application you must use:
%WINDIR%\System32\odbcad32.exe
From https://robertoschiabel.wordpress.com/2008/02/28/windows-x64-32bit-odbc-vs-64bit-odbc/amp/
Install the 64-bit Access DB Driver
The driver for accessing accdb files might not be available on your system. In that case, you are going to need to install the driver from Microsft. The accdb driver is available for download from Microsoft here:
https://microsoft.com/en-US/download/details.aspx?id=13255
Use 32-bit Python (if Access DB driver isn't available in 64-bit)
Another alternative if you have the 32-bit accdb ODBC driver already installed on your system is to use the 32-bit version of Python. An example of this type of environment might be if you had a 32-bit version of Office installed already.
Install the 32-bit version of Windows Python 3.7.0 from:
https://www.python.org/ftp/python/3.7.0/python-3.7.0.exe
When using the 32-bit version of python, you should access the DSNs under the 32-bit ODBC Data Source Administrator.
Connect to the Named DSN
import pyodbc
conn = pyodbc.connect(r'DSN=myDsn')

PyODBC "Image not found (0) (SQLDriverConnect)"

I'm trying to use PyODBC to connect to an Access database. It works fine on Windows, but running it under OS X I get—
Traceback (most recent call last):
File "", line 1, in
File "access.py", line 10, in init
self.connection = connect(driver='{Microsoft Access Driver (.mdb)}', dbq=path, pwd=password)
pyodbc.Error: ('00000', '[00000] [iODBC][Driver Manager]dlopen({Microsoft Access Driver (.mdb)}, 6): image not found (0) (SQLDriverConnect)')
Do I have to install something else? Have I installed PyODBC wrong?
Thanks
pyodbc allows connecting to ODBC data sources, but it does not actually implements drivers.
I'm not familiar with OS X, but on Linux ODBC sources are typically described in odbcinst.ini file (location is determined by ODBCSYSINI variable).
You will need to install Microsoft Access ODBC driver for OS X.

Categories