使用python比较两个sqlite3表 [英] Comparing two sqlite3 tables using python

查看:47
本文介绍了使用python比较两个sqlite3表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在问题有点棘手....我有2个表,我想将它们的内容进行比较.这些表有相同的编号.列,相同的列名称和相同的列顺序(如果有的话).

Now the question is a little tricky.... I have 2 tables that i want to compare them for their content. The tables have same no. of columns and same column names and same ordering of columns(if there is such thing).

现在我想比较它们的内容,但诀窍是它们的行的顺序可以不同,即行号.表1中的1可以出现在行1中.表2中的1000.我想比较它们的内容,以使行的顺序无关紧要.还要记住,它们不是主键.

Now i want to compare their contents but the trick is the ordering of their rows can be different ,i.e., row no. 1 in table 1 can be present in row no. 1000 in table 2. I want to compare their contents such that the ordering of the rows don't matter. And also remember that their is no such thing as primary key.

现在,我可以使用和设计数据结构,也可以使用现有的库来完成这项工作.我想使用一些现有的API(如果有).那么any1可以向我指出正确的方向吗?

Now i can use and design Data structures or i can use an existing library to do the job. I want to use some existing APIs (if any). So can any1 point me in the right direction??

推荐答案

制作两个文本文件.对它们进行排序.将它们与 diff 进行比较.

Make two text files. Sort them. Compare them with diff.

或者,将它们导入到SQLite表中.然后,您可以使用如下查询:

Alternatively, import them into SQLite tables. Then you can use queries like the following:

SELECT * FROM a INTERSECT SELECT * FROM b;
SELECT * FROM a EXCEPT    SELECT * FROM b;

获取在两个表中或仅在一个表中存在的行.

to get rows that exist in both tables, or only in one table.

这篇关于使用python比较两个sqlite3表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆