英文:
SQLAlchemy: proper way to specify connection URL in python
问题
使用SQLAlchemy(版本2.0.4)通过pymysql驱动程序从Python连接到远程MySQL服务器。
如果我以“简单”的方式声明连接URL,它可以正常工作:
import sqlalchemy as db
engine = db.create_engine("mysql+pymysql://dbuser:verysecret@myhost.com:9001/mydb")
但我更喜欢在教程中找到的这个版本:
import sqlalchemy as db
connect_url = db.engine.url.URL(
"mysql+pymysql",
username="dbuser",
password="verysecret",
host="myhost.com",
port="9001",
database="mydb",
)
engine = db.create_engine(connect_url)
但是在connect_url行会导致以下错误:
Traceback (most recent call last):
File "/Users/bob/Documents/work/code/mysqltest/mysqltest/sqlalch.py", line 3, in <module>
connect_url = db.engine.url.URL(
TypeError: URL.__new__() missing 1 required positional argument: 'query'
编辑:感谢@snakecharmer的回答,我随后尝试添加有效的查询参数如下:
import sqlalchemy as db
connect_url = db.engine.url.URL(
"mysql+pymysql",
username="root",
password="ghj12WQ_AA1997",
host="isagog.com",
port=9001,
database="athena",
query=dict(charset="utf8mb4"),
)
engine = db.create_engine(connect_url)
这样可以正常工作。但仍然不确定为什么如果不提供查询参数就会出错。
英文:
Using SQLAlchemy (version 2.0.4)to connect to a remote MySQL server from python via the pymysql driver.
If I declare my connection URL in the "simple" way, it works:
import sqlalchemy as db
engine = db.create_engine("mysql+pymysql://dbuser:verysecret@myhost.com:9001/mydb")
but I'd prefer this other version I've found in a tutorial:
import sqlalchemy as db
connect_url = db.engine.url.URL(
"mysql+pymysql",
username="dbuser",
password="verysecret",
host="myhost.com",
port="9001",
database="mydb",
)
engine = db.create_engine(connect_url)
but this fails on the connect_url line as follows:
Traceback (most recent call last):
File "/Users/bob/Documents/work/code/mysqltest/mysqltest/sqlalch.py", line 3, in <module>
connect_url = db.engine.url.URL(
TypeError: URL.__new__() missing 1 required positional argument: 'query'
EDIT: thanks to @snakecharmer's reply below I then tried to add a valid query parameter as follows:
import sqlalchemy as db
connect_url = db.engine.url.URL(
"mysql+pymysql",
username="root",
password="ghj12WQ_AA1997",
host="isagog.com",
port=9001,
database="athena",
query=dict(charset="utf8mb4"),
)
engine = db.create_engine(connect_url)
And this does works well. But still not sure why I get an error if the query parameter is not provided.
答案1
得分: 1
如果不需要传递任何查询参数,请传递一个空字符串或字典作为查询部分:
>>> url = sa.engine.URL('mysql+pymysql', 'root', 'root', 'localhost', 3306, 'test', {})
>>> url
mysql+pymysql://root:***@localhost:3306/test
如果需要传递查询参数,请将它们作为字典传递:
>>> import sqlalchemy as sa
>>>
>>> url = sa.engine.URL('mysql+pymysql', 'root', 'root', 'localhost', 3306, 'test', {'charset': 'utf8mb4'})
>>> url
mysql+pymysql://root:***@localhost:3306/test?charset=utf8mb4
英文:
If you don't need to pass any query parameters, pass an empty string or dict for query:
>>> url = sa.engine.URL('mysql+pymysql', 'root', 'root', 'localhost', 3306, 'test', {})
>>> url
mysql+pymysql://root:***@localhost:3306/test
If you need to pass query arguments, pass them as a dictionary:
>>> import sqlalchemy as sa
>>>
>>> url = sa.engine.URL('mysql+pymysql', 'root', 'root', 'localhost', 3306, 'test', {'charset': 'utf8mb4'})
>>> url
mysql+pymysql://root:***@localhost:3306/test?charset=utf8mb4
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论