获取 SQL 数据时出错
P粉648469285
P粉648469285 2023-09-04 17:09:42
[MySQL讨论组]
<p>所以我只是从数据库中提取数据,遇到以下错误。 代码如下:</p> <pre class="brush:php;toolbar:false;">import pandas as pd import sqlalchemy engine = sqlalchemy.create_engine(&quot;mysql+pymysql://root:@localhost:3306/haider&quot;) df = pd.read_sql_table(&quot;ahmed&quot;, engine)</pre> <p>运行此代码时出现以下错误:</p> <pre class="brush:php;toolbar:false;">--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In [46], line 1 ----&gt; 1 df = pd.read_sql_table(&quot;ahmed&quot;, engine) 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) 282 raise ValueError(f&quot;Table {table_name} not found&quot;) 284 # error: Item &quot;SQLiteDatabase&quot; of &quot;Union[SQLDatabase, SQLiteDatabase]&quot; 285 # has no attribute &quot;read_table&quot; --&gt; 286 table = pandas_sql.read_table( # type: ignore[union-attr] 287 table_name, 288 index_col=index_col, 289 coerce_float=coerce_float, 290 parse_dates=parse_dates, 291 columns=columns, 292 chunksize=chunksize, 293 ) 295 if table is not None: 296 return table 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) 1417 &quot;&quot;&quot; 1418 Read SQL database table into a DataFrame. 1419 (...) 1457 1458 &quot;&quot;&quot; 1459 table = SQLTable(table_name, self, index=index_col, schema=schema) -&gt; 1460 return table.read( 1461 coerce_float=coerce_float, 1462 parse_dates=parse_dates, 1463 columns=columns, 1464 chunksize=chunksize, 1465 ) File E:\python\lib\site-packages\pandas\io\sql.py:1003, in SQLTable.read(self, coerce_float, parse_dates, columns, chunksize) 1001 else: 1002 sql_select = select(self.table) -&gt; 1003 result = self.pd_sql.execute(sql_select) 1004 column_names = result.keys() 1006 if chunksize is not None: File E:\python\lib\site-packages\pandas\io\sql.py:1405, in SQLDatabase.execute(self, *args, **kwargs) 1403 def execute(self, *args, **kwargs): 1404 &quot;&quot;&quot;Simple passthrough to SQLAlchemy connectable&quot;&quot;&quot; -&gt; 1405 return self.connectable.execution_options().execute(*args, **kwargs) AttributeError: 'OptionEngine' object has no attribute 'execute'</pre> <p>注意:我的数据库没有任何密码,其次我也已经安装了 PyMySQL。</p> <p>我试图解释使用 python 获取 sql 数据时遇到的错误,我希望得到该问题的解决方案。</p>
P粉648469285
P粉648469285

全部回复(1)
P粉781235689

我强烈建议您尝试以下操作

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

我认为您可能缺少最后的 connect() 部分。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号