gpt4 book ai didi

python - 连接到 python 中的数据库 url,其中 url 用于 jdbc

转载 作者:行者123 更新时间:2023-11-29 02:33:51 31 4
gpt4 key购买 nike

我正在尝试编写一个将连接到数据库的 Python 脚本。数据库的主机 URL 以 jdbc 格式提供给我:

URL = jdbc:mysql://www.myurl.com:3306/

我不知道如何将该 URL 翻译成 pythonese。

如有任何帮助,我们将不胜感激。谢谢

import MySQLdb

conn = MySQLdb.connect(host="????",user="web3u4",passwd="password",db="web3db4")

cursor = conn.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "server version:", row[0]
cursor.close ()

# disconnect from server
db.close()

最佳答案

你应该使用 urlparse.urlparse解析 jdbc 字符串。

from urlparse import urlparse
jdbc = "jdbc:mysql://www.myurl.com:3306"
result= urlparse(jdbc)


MySQLdb.connect(host=result.host,
user=result.username,
passwd=result.password,
db="web3db4")

不确定您打算如何传递数据库。但如果您添加它,我也可以帮助您。

关于python - 连接到 python 中的数据库 url,其中 url 用于 jdbc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8380909/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com