Error While Fetching SQL data

huangapple go评论104阅读模式
英文:

Erro While Fetching SQL data

问题

I encountered the following error while running the provided code:

  1. AttributeError: 'OptionEngine' object has no attribute 'execute'

It seems that you encountered an AttributeError related to the 'execute' attribute of the 'OptionEngine' object. You mentioned that your database doesn't have a password, and you have PyMySQL installed. To resolve this issue, you might need to check the compatibility between the SQLAlchemy version and PyMySQL, or there might be an issue with your SQLAlchemy engine configuration.

For a more detailed solution, you may want to verify your SQLAlchemy and PyMySQL versions and ensure your database connection is correctly configured.

英文:

so i was just extracting data from my database and i encountered following error.
The code goes as follows:

  1. import pandas as pd
  2. import sqlalchemy
  3. engine = sqlalchemy.create_engine("mysql+pymysql://root:@localhost:3306/haider")
  4. df = pd.read_sql_table("ahmed", engine)

while running this code i got following error:

  1. ---------------------------------------------------------------------------
  2. AttributeError Traceback (most recent call last)
  3. Cell In [46], line 1
  4. ----> 1 df = pd.read_sql_table("ahmed", engine)
  5. File E:\python\lib\site-packages\pandas\io\sql.py:286, in read_sql_table(table_name, con, schema, index_col, coerce_float, parse_dates, columns, chunksize)
  6. 282 raise ValueError(f"Table {table_name} not found")
  7. 284 # error: Item "SQLiteDatabase" of "Union[SQLDatabase, SQLiteDatabase]"
  8. 285 # has no attribute "read_table"
  9. --> 286 table = pandas_sql.read_table( # type: ignore[union-attr]
  10. 287 table_name,
  11. 288 index_col=index_col,
  12. 289 coerce_float=coerce_float,
  13. 290 parse_dates=parse_dates,
  14. 291 columns=columns,
  15. 292 chunksize=chunksize,
  16. 293 )
  17. 295 if table is not None:
  18. 296 return table
  19. File E:\python\lib\site-packages\pandas\io\sql.py:1460, in SQLDatabase.read_table(self, table_name, index_col, coerce_float, parse_dates, columns, schema, chunksize)
  20. 1417 """
  21. 1418 Read SQL database table into a DataFrame.
  22. 1419
  23. (...)
  24. 1457
  25. 1458 """
  26. 1459 table = SQLTable(table_name, self, index=index_col, schema=schema)
  27. -> 1460 return table.read(
  28. 1461 coerce_float=coerce_float,
  29. 1462 parse_dates=parse_dates,
  30. 1463 columns=columns,
  31. 1464 chunksize=chunksize,
  32. 1465 )
  33. File E:\python\lib\site-packages\pandas\io\sql.py:1003, in SQLTable.read(self, coerce_float, parse_dates, columns, chunksize)
  34. 1001 else:
  35. 1002 sql_select = select(self.table)
  36. -> 1003 result = self.pd_sql.execute(sql_select)
  37. 1004 column_names = result.keys()
  38. 1006 if chunksize is not None:
  39. File E:\python\lib\site-packages\pandas\io\sql.py:1405, in SQLDatabase.execute(self, *args, **kwargs)
  40. 1403 def execute(self, *args, **kwargs):
  41. 1404 """Simple passthrough to SQLAlchemy connectable"""
  42. -> 1405 return self.connectable.execution_options().execute(*args, **kwargs)
  43. AttributeError: 'OptionEngine' object has no attribute 'execute'

NOTE: My database doesn't have any password and secondly i was already installed PyMySQL too.

i tried to explain an error that i got while fetching sql data using python and i expect to get a solution to that problem.

答案1

得分: 1

  1. 我建议你尝试以下代码部分
  2. engine = sqlalchemy.create_engine("mysql+pymysql://root:@localhost:3306/haider").connect()

我认为你可能遗漏了末尾的 connect() 部分。

英文:

I urge you to try the following

  1. engine = sqlalchemy.create_engine("mysql+pymysql://root:@localhost:3306/haider").connect()

I think you might be missing the connect() part at the end.

huangapple
  • 本文由 发表于 2023年4月11日 10:12:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定