【问题标题】:Extract from a database从数据库中提取
【发布时间】:2016-03-08 03:22:01
【问题描述】:

我有一个需要从中提取的数据库。该数据库有以下表格:

商店
YELP 商家列表,带有姓名、电话号码等。
还有locationid

地点
包含名称(伦敦、纽约)、locationID、parentid
ParentID 允许表处于层次结构中。

例如。纽约州和纽约市可能有一个位置
在这种情况下,NYC 的 parentid 将是纽约州的对象。

类别
类别有一个 ID 和名称
例如。汽车经销商,船只经销商)

商店类别
每个商家都有多个 BusinessCategories
包含 businessid 和 categoryid。

然后,我们根据位置和业务类别提供大量搜索结果页面。

例如。

纽约市所有业务类别
纽约市船只经销商
纽约市汽车经销商
纽约州所有业务类别
纽约州船舶经销商
纽约州汽车经销商

注意:搜索结果将显示该位置和任何子位置的商家。
示例:纽约州将显示在纽约市上市的企业。

我需要编写一个 python 脚本,让我知道 2 个搜索结果页面是否列出了相同的企业。

示例:如果纽约州的所有船只经销商都存在于纽约市内,那么两个页面:

纽约州船只经销商
纽约市船只经销商

将列出相同的企业。

我需要一些关于如何有效解决问题的一般建议

mysql> DESC categories;
+--------------+------------------+------+-----+-------------------+-----------------------------+
| Field        | Type             | Null | Key | Default           | Extra                       |   
+--------------+------------------+------+-----+-------------------+-----------------------------+
| CategoryID   | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |   
| CategoryName | char(50)         | NO   |     |                   |                             |   
| timestamp    | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+--------------+------------------+------+-----+-------------------+-----------------------------+
3 rows in set (0.07 sec)

mysql> DESC shopcategories;
+----------------+------------------+------+-----+-------------------+-----------------------------+
| Field          | Type             | Null | Key | Default           | Extra                       |   
+----------------+------------------+------+-----+-------------------+-----------------------------+
| ShopCategoryID | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |   
| ShopID         | int(10) unsigned | YES  | MUL | NULL              |                             |   
| CategoryID     | int(10) unsigned | YES  | MUL | NULL              |                             |   
| timestamp      | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+----------------+------------------+------+-----+-------------------+-----------------------------+
4 rows in set (0.00 sec)

mysql> DESC locations;
+-----------------+------------------+------+-----+-------------------+-----------------------------+
| Field           | Type             | Null | Key | Default           | Extra                       |   
+-----------------+------------------+------+-----+-------------------+-----------------------------+
| LocationID      | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |   
| LocationName    | char(50)         | YES  |     | NULL              |                             |   
| LocationUrlName | varchar(100)     | YES  |     | NULL              |                             |   
| ParentID        | int(10) unsigned | YES  |     | NULL              |                             |   
| timestamp       | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| postcode        | char(5)          | YES  |     | NULL              |                             |   
+-----------------+------------------+------+-----+-------------------+-----------------------------+
6 rows in set (0.00 sec)
mysql> 
mysql> desc shops;
+---------------------+------------------+------+-----+-------------------+-----------------------------+
| Field               | Type             | Null | Key | Default           | Extra                       |   
+---------------------+------------------+------+-----+-------------------+-----------------------------+
| ShopID              | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |   
| ContactAddressID    | int(10) unsigned | NO   | MUL | 0                 |                             |   
| OwnerID             | int(10) unsigned | NO   | MUL | 0                 |                             |   
| ShopName            | int(10) unsigned    | Null  | MUL | 109              |                             |   
| LocationID           | varchar(50)      | NO   |     |                   |                             |   
| ShopUrlName         | varchar(300)     | YES  |     | NULL              |                             |                |                             |

【问题讨论】:

  • 你的问题最终解决了吗?

标签: python sql


【解决方案1】:

我建议使用oursql 模块。它非常简单且功能强大。

这里是教程:https://pythonhosted.org/oursql/index.html


基本步骤

连接数据库

conn = oursql.connect(host='127.0.0.1', user='habnabit', passwd='foobar',
    db='example', port=3307)

创建游标并执行查询

curs = conn.cursor(oursql.DictCursor)
curs.execute(
    'SELECT * FROM `some_table` WHERE `col1` = ? AND `col2` = ?',
    (42, -3))

获取结果

curs.fetchall()

所有这些示例均取自教程。如果你想了解更多关于这些类及其方法的信息,这里是 API 参考: https://pythonhosted.org/oursql/api.html#


为了您的比较...我只是比较结果的 ID。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    相关资源
    最近更新 更多