比较Python 3中跨数据库的SQLite表中的值

时间:2017-02-03 16:53:28

标签: python database sqlite python-3.x

我正在尝试编写一个方法,将给定SQLite数据库表中的值与另一个预先存在的数据库表进行比较。我已经为给定的数据库编写了一个“describe”方法,对于数据库中的每个表,将以下元组附加到数组中:

databaseInfo.append((table, numberOfColumns, numberOfRows, numberOfCells))

现在我试图遍历每个表的列和行,以查看我的预先存在的数据库中是否存在该值。在这样做时,我设置了以下变量:

tables = [str(i[0]) for i in databaseInfo]
columns = [int(i[1]) for i in databaseInfo]
rows = [int(i[2]) for i in databaseInfo]
cells = [int(i[3]) for i in databaseInfo]

所以我要问的是如何使用sqlite命令迭代数据库的单元格并进行比较。我正在寻找我将如何能够这样做的逻辑/语法。

例如,如果给我一个包含4个表的数据库,我想创建一个方法,对于每个表,它将运行并检查每个单元格的值是否已经存在于我的数据库中。所以我想做类似以下的事情:

假设我们有两张桌子

我的表:

+----+-------+-----+---------------+   
| pk | name  | age |  occupation   |
+----+-------+-----+---------------+
|  0 | Joey  |  17 | Student       |
|  1 | Jerry |  18 | Student       |
|  2 | Gabe  |  20 | Mathematician |
|  3 | Ian   |  34 | Unemployed    |
|  4 | Rob   |  33 | Teacher       |
|  5 | Harry |  18 | Student       |
|  6 | Joey  |  20 | Economist     |
|  7 | Jerry |  33 | Coach         |
+----+-------+-----+---------------+

给出表:

+----+----------------+--------------------+---------------+
| pk | Husband        | height(decimeters) |  occupation   |
+----+----------------+--------------------+---------------+
|  0 | Joey           |  16                | Student       |
|  1 | Jim            |  17                | Programmer    |
|  2 | Joey           |  16                | Teacher       |
|  3 | Jerry          |  20                | Unemployed    |
|  4 | Tim            |  13                | Unemployed    |
|  5 | Harry          |  18                | Programmer    |
|  6 | Steve          |  18                | Economist     |
|  7 | Jerry          |  11                | Painter       |
+----+----------------+--------------------+---------------+

鉴于我可以使用我的python方法描述给定的表,我想为每个数据库创建游标,读取给定的表并能够识别和更改相对于我的数据库冗余的值。例如,假设我想用“冗余”替换每个冗余值!返回这样的表:

ColumnsToExclude = pk 

╔════╦════════════╦════════════════════╦════════════╗
║ pk ║  Husband   ║ height(decimeters) ║ occupation ║
╠════╬════════════╬════════════════════╬════════════╣
║  0 ║ redundant! ║ 16                 ║ redundant! ║
║  1 ║ Jim        ║ redundant!         ║ Programmer ║
║  2 ║ redundant! ║ 16                 ║ redundant! ║
║  3 ║ redundant! ║ redundant!         ║ redundant! ║
║  4 ║ Tim        ║ 13                 ║ redundant! ║
║  5 ║ redundant! ║ redundant!         ║ Programmer ║
║  6 ║ Steve      ║ redundant!         ║ redundant! ║
║  7 ║ redundant! ║ 11                 ║ Painter    ║
╚════╩════════════╩════════════════════╩════════════╝

1 个答案:

答案 0 :(得分:0)

可以构造一个SQL查询来查找另一个表的相应列中的值:

SELECT pk,
       CASE
         WHEN Husband    IN (SELECT Husband    FROM MyTable)
         THEN 'redundant!' ELSE Husband
       END AS Husband,
       CASE
         WHEN height     IN (SELECT age        FROM MyTable)
         THEN 'redundant!' ELSE height
       END AS height,
       CASE
         WHEN occupation IN (SELECT occupation FROM MyTable)
         THEN 'redundant!' ELSE occupation
       END AS occupation
FROM GivenTable;

必须根据您要匹配的表格自定义表名和列名。