【问题标题】:How to write an incremental query by steps with sqlite and python?如何使用 sqlite 和 python 逐步编写增量查询?
【发布时间】:2019-09-22 06:47:01
【问题描述】:

我是 sqlite 和 python 编程的新手。 我想用 50 步增量地在表上编写一个选择查询(根据 where 子句选择前 50 条记录,第二条 50 条直到最后一条记录)。

对于这个porpuse,我使用了'limit clause',我获取了50条记录。但它只获取前 50 条记录。

cur.execute(""" SELECT * FROM Log_Table WHERE name= 'x  limit 50 """)
print(cur.fetchall())

我想在一个包含 50 条记录的循环中选择记录。 我通过range 函数尝试了它,但它不起作用。

【问题讨论】:

标签: python sqlite select


【解决方案1】:

基本上你希望..... LIMIT 50 OFFSET 0 用于第一个然后LIMIT 50 OFFSET 50 用于下一个直到LIMIT 50 OFFSET a_number_as_big_or_bigger_than_the_number_of_rows 在这种情况下将返回0 行。

也许考虑以下情况,其中引入了一个额外的控制表,这可以使事情变得灵活且相当简单:-

/* Just in case the tables exist */
DROP TABLE IF EXISTS mainTable;
DROP TABLE IF EXISTS iterationControl;

/* The table that will be progressively interrogated */
CREATE TABLE IF NOT EXISTS mainTable(aColumn INTEGER PRIMARY KEY);

/* The control table, could perhaps be TEMP table*/
CREATE TABLE IF NOT EXISTS iterationControl (chunkSize INTEGER, chunkStart INTEGER); 
DELETE FROM iterationControl; /* Just in case, clear any rows */
INSERT INTO iterationControl VALUES(50,0,0); /* initialise the iteration table */

/* reset the chunk size for less out for this example only */
UPDATE iterationControl SET chunkSize = 5;
/* reset to the start (not needed here but as an example of how to)*/
UPDATE iterationControl SET chunkStart = 0;

/* Load some data purely included for testing purposes */
WITH RECURSIVE cte1(val) AS (SELECT 1 UNION ALL SELECT val+1 FROM cte1 LIMIT 10000)
INSERT INTO mainTable SELECT val FROM cte1;

/**START OF TESTING**/
/* First grab */
SELECT * FROM mainTable LIMIT (SELECT chunkSize FROM iterationControl) OFFSET (SELECT chunkStart FROM iterationControl);
/* Post run update*/
UPDATE iterationControl SET chunkStart = chunkStart + chunkSize;

/* 2nd grab */
SELECT * FROM mainTable LIMIT (SELECT chunkSize FROM iterationControl) OFFSET (SELECT chunkStart FROM iterationControl);
/* Post run update modified to skip till past the end ONLY FOR EASE OF DEMO */
UPDATE iterationControl SET chunkStart = chunkStart + chunkSize + 100000;

/* 3rd grab (mimic last) */
SELECT * FROM mainTable LIMIT (SELECT chunkSize FROM iterationControl) OFFSET (SELECT chunkStart FROM iterationControl);

/* Reset iteration Control to start from the begining*/
UPDATE iterationControl SET chunkStart = 0;
/* First of a new set of grabs */
SELECT * FROM mainTable LIMIT (SELECT chunkSize FROM iterationControl) OFFSET (SELECT chunkStart FROM iterationControl);
UPDATE iterationControl SET chunkStart = chunkStart + chunkSize;

/* change size of data part way through from 5 to 13 rows per grab */
UPDATE iterationControl SET chunkSize = 13;
/* 2nd grab now at 13 per grab */
SELECT * FROM mainTable LIMIT (SELECT chunkSize FROM iterationControl) OFFSET (SELECT chunkStart FROM iterationControl);
/* no change to post run even though different grab size */
UPDATE iterationControl SET chunkStart = chunkStart + chunkSize;

/* 3rd grab (just to show that post update worked correctly) */
SELECT * FROM mainTable LIMIT (SELECT chunkSize FROM iterationControl) OFFSET (SELECT chunkStart FROM iterationControl);
UPDATE iterationControl SET chunkStart = chunkStart + chunkSize;


/* clean up the database as I do not want these tables hanging around */
DROP TABLE IF EXISTS mainTable;
DROP TABLE IF EXISTS iterationControl;

结果

以上结果

1。第一次抓取(根据控件 5 行)

2。第二次抢(下 5 次)

3。最后一次抓取(没有行结束(在本例中为过去))

4。重置后开始(先抢一遍)

5。在飞行中更改为抓取/卡盘尺寸(从 5 到 13)

6。再次抓取 13 个,只是为了确认完全相同的帖子更新工作正常

【讨论】:

  • 感谢 MikeT。我会试试。我使用 python 来选择和更新一个表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多