我正在尝试编写一个将连接到数据库的 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/