简体   繁体   English

使用 Python 的远程数据库连接

[英]Remote database connection using Python

I have a Python programm which is scraping some values from my localhost.我有一个 Python 程序,它正在从我的本地主机中抓取一些值。 My aim is to store that values and to insert them into my live Website.我的目标是存储这些值并将它们插入到我的实时网站中。 To be more specific, i would like to insert those values into my live website database using my Python programm.更具体地说,我想使用我的 Python 程序将这些值插入到我的实时网站数据库中。 The parser looks like that解析器看起来像这样

    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

Now from this point i would like to connect to my live database and insert the "id".How is it possible with Python to have access in my remote Database and what techniques should i follow?现在,我想连接到我的实时数据库并插入“id”。Python 怎么可能访问我的远程数据库,我应该遵循哪些技术?

You'll probably want to use the DB-API , and specifically for MySQL, the MySQLdb module .您可能想要使用DB-API ,特别是 MySQLMySQLdb模块 This is nonstandard, so you'll have to install it.这是非标准的,因此您必须安装它。 First, you connect:首先,您连接:

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')

After you've connected, it's pretty simple to insert a row:连接后,插入一行非常简单:

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

When you're done with the connection, don't forget to close it.完成连接后,不要忘记关闭它。

conn.close()

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

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