Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建';发动机&x27;Python中的对象?_Python_Mysql_Pandas_Sqlclient_Database Cursor - Fatal编程技术网

创建';发动机&x27;Python中的对象?

创建';发动机&x27;Python中的对象?,python,mysql,pandas,sqlclient,database-cursor,Python,Mysql,Pandas,Sqlclient,Database Cursor,我使用的是Python 3.7,我不太熟悉Python中的SQL,我有以下代码来创建SQL引擎: mysql_engine = sqlalchemy.create_engine('mysql://xxxxxx') 然后我有一个预定义的SQL查询,这里是一个简化的版本:(这个查询我在MySQL工作台上测试过,效果很好) 我希望能够从表中读取/查询,我尝试了: cursor = mysql_engine.cursor() cursor.execute(example_query) 但是我得到了一

我使用的是Python 3.7,我不太熟悉Python中的SQL,我有以下代码来创建SQL引擎:

mysql_engine = sqlalchemy.create_engine('mysql://xxxxxx')
然后我有一个预定义的SQL查询,这里是一个简化的版本:(这个查询我在MySQL工作台上测试过,效果很好)

我希望能够从表中读取/查询,我尝试了:

cursor = mysql_engine.cursor()
cursor.execute(example_query)
但是我得到了一个错误:AttributeError:“Engine”对象没有属性“cursor”,也许我可以使用
pandas
?有人能帮忙吗,谢谢。

pd.read\u sql
第二个参数可以接受sqlalchemy引擎

con:使用SQLAlchemy的SQLAlchemy可连接、str或sqlite3连接 可以使用该库支持的任何数据库。如果是DBAPI2 对象,仅支持sqlite3。用户对发动机负责 SQLAlchemy可连接设备的处理和连接关闭;str 连接将自动关闭


您好,我尝试了一下,但遇到了一个错误:
sqlalchemy.exc.OperationalError:(MySQLdb.\u exceptions.OperationalError)(2002,“无法连接到“aws rds实例端点名称”(10060)”上的MySQL服务器)(此错误的背景信息位于:http://sqlalche.me/e/13/e3q8)
这是一个连接错误。可能是您的连接字符串。我也帮不了你。不过,我发布的内容回答了您最初的问题。一旦你有了一个正常工作的引擎,使用
pd.read\u sql
就可以完成剩下的工作。有人知道如何确保mysql\u引擎在完成后关闭吗?它是自动发生的还是需要显式的?@Lisle简而言之,如果你想确保你的连接已关闭,你应该注意关闭它。
cursor = mysql_engine.cursor()
cursor.execute(example_query)
pd.read_sql(example_query, mysql_engine)