如何使用PyCharm连接MySQL Workbench数据库?

问题描述 投票:0回答:1

我正在尝试使用 PyCharm 通过 Python 连接到 MySQL 数据库。我正在使用以下代码。

from errno import errorcode

import pandas as pd
import pypyodbc
import mysql.connector
from mysql.connector import Error
import os

os.environ['LD_LIBRARY_PATH'] = os.getcwd()

team_df = pd.read_excel('sampledata.xlsx', sheet_name='Team')

cnx = mysql.connector.connect(host='localhost',
                              database='SoccerDatabase',
                              user='root',
                              password='')
if cnx.is_connected():
    print("Connection Established")
cnx.close()

但是,我无法连接并收到以下错误:

import pandas as pd
Traceback (most recent call last):
  File "/Users/bansarivadgama/Desktop/integrationfactory/DataModellingIntegrationFactory/venv/lib/python3.11/site-packages/pypyodbc.py", line 428, in <module>
    ODBC_API = ctypes.cdll.LoadLibrary('libodbc.so')
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ctypes/__init__.py", line 454, in LoadLibrary
    return self._dlltype(name)
           ^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ctypes/__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: dlopen(libodbc.so, 0x0006): tried: 'libodbc.so' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibodbc.so' (no such file), '/usr/lib/libodbc.so' (no such file, not in dyld cache), 'libodbc.so' (no such file), '/usr/lib/libodbc.so' (no such file, not in dyld cache)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/bansarivadgama/Desktop/integrationfactory/DataModellingIntegrationFactory/dataimport.py", line 5, in <module>
    import pypyodbc
  File "/Users/bansarivadgama/Desktop/integrationfactory/DataModellingIntegrationFactory/venv/lib/python3.11/site-packages/pypyodbc.py", line 440, in <module>
    raise OdbcNoLibrary('ODBC Library is not found. Is LD_LIBRARY_PATH set?')
pypyodbc.OdbcNoLibrary: 'ODBC Library is not found. Is LD_LIBRARY_PATH set?'

Process finished with exit code 1

有人可以帮我找出解决方案吗?

python pycharm environment-variables mysql-connector-python
1个回答
0
投票

通过安装以下库可以解决该错误:

brew install unixodbc
© www.soinside.com 2019 - 2024. All rights reserved.