繁体   English   中英

使用 Python 的远程数据库连接

[英]Remote database connection using Python

我有一个 Python 程序,它正在从我的本地主机中抓取一些值。 我的目标是存储这些值并将它们插入到我的实时网站中。 更具体地说,我想使用我的 Python 程序将这些值插入到我的实时网站数据库中。 解析器看起来像这样

    from bs4 import BeautifulSoup

    import urllib

    x=urllib.urlopen("http://localhost/askisi2.html")
    s = x.read()
    soup = BeautifulSoup(s)

    m = soup.find("div",{"id":"s_number"})
    id = m.text
    print id

现在,我想连接到我的实时数据库并插入“id”。Python 怎么可能访问我的远程数据库,我应该遵循哪些技术?

您可能想要使用DB-API ,特别是 MySQLMySQLdb模块 这是非标准的,因此您必须安装它。 首先,您连接:

import MySQLdb
# other parameters are available; for details, see
# http://mysql-python.sourceforge.net/MySQLdb.html#functions-and-attributes
conn = MySQLdb.connect(user='someone', passwd='hunter2', db='some_database')

连接后,插入一行非常简单:

conn.execute('insert into some_table (id) values (?)', (id,))

完成连接后,不要忘记关闭它。

conn.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM