我创建了一个非常简单的脚本,该脚本将SQLAlchemy连接到MySQL数据库并运行SELECT语句。它在我的开发笔记本电脑上运行良好。现在,将其移动到新服务器上,它就坏了。
from sqlalchemy import create_engine
import pandas as pd
def return_connection_string(database_name, database_user, database_password, database_host, database_port):
try:
connection_string = 'mysql+mysqlconnector://' + database_user + ':' + \
database_password + '@' + database_host + ':' + database_port + '/' + database_name
print(connection_string)
return connection_string
except Exception as e:
print('Encountered error while generating connection string for MySQL!')
print(e)
def return_connection_object(database_name, database_user, database_password, database_host, database_port):
try:
connection_string = return_connection_string(
database_name, database_user, database_password, database_host, database_port)
engine = create_engine(connection_string).connect()
return engine
except Exception as e:
print('Encountered error while connecting to MySQL database!')
print(e)
mysql_conn = return_connection_object(
'xyz', 'xyz', 'xyz', 'xyz', 'xyz')
SQL_Query = pd.read_sql_query(
'select current_date as today, current_date-1 as yesterday', mysql_conn)
df = pd.DataFrame(SQL_Query, columns=['today', 'yesterday'])
df1 = df[['yesterday']]
print(df1)
mysql_conn.close()
在运行时,脚本因该错误而失败
Encountered error while connecting to MySQL database!
No module named 'mysql'
AttributeError: 'NoneType' object has no attribute 'cursor'
我将服务器与开发笔记本电脑进行了比较,PIP向我展示了相同版本的SQLAlchemy模块:1.3.11。
我需要安装其他模块吗?我想念什么?
答案 0 :(得分:1)
查看服务器和开发笔记本电脑上的PIP输出后,我发现缺少一个模块。运行此命令
pip3 install --upgrade mysql-connector-python
解决了问题。