遍历python sqlite数据库

Hot*_*non 1 python sqlite python-3.x

我正在尝试遍历 SQLite 数据库并对列表中的对象执行检查或操作。我需要使用数据库,因为最终的对象数量将非常大,并且所有操作本质上都是串行的(在基本排序之后)。

我的问题是如何遍历列表并在检查对象的某些质量后将其放入新的数据库对象中?我想执行几次串行“检查”,其中一次最多将两个对象带入内存,然后重新分配。

下面是我的代码示例。当我运行最后一个操作时,我无法“重新运行”相同的循环。我怎样才能不只是打印对象,而是将其保存到新数据库中?

import os
import sqlite3 as lite
import sys
import random
import gc
import pprint

def make_boxspace():

    refine_zone_cube_size = 1

    refine_zone_x1 = 1*refine_zone_cube_size
    refine_zone_y1 = 1*refine_zone_cube_size
    refine_zone_z1 = 1*refine_zone_cube_size
    refine_zone_x2 = refine_zone_x1+(2*refine_zone_cube_size)
    refine_zone_y2 = refine_zone_y1+(1*refine_zone_cube_size)
    refine_zone_z2 = refine_zone_z1+(1*refine_zone_cube_size)

    point_pass_length = (1.0/4.0)

    outlist = []
    for i in range(int((refine_zone_x2-refine_zone_x1)/point_pass_length)):
        for j in range(int((refine_zone_y2-refine_zone_y1)/point_pass_length)):
            for k in range(int((refine_zone_z2-refine_zone_z1)/point_pass_length)):

                if (random.random() > 0.5):
                    binary = True
                else:
                    binary = False

                if binary:
                    x1 = point_pass_length*i
                    y1 = point_pass_length*j
                    z1 = point_pass_length*k
                    x2 = x1+point_pass_length
                    y2 = y1+point_pass_length
                    z2 = z1+point_pass_length

                    vr_lev = int(random.random()*3)

                    outlist.append([\
                    float(str("%.3f" % (x1))),\
                    float(str("%.3f" % (y1))),\
                    float(str("%.3f" % (z1))),\
                    float(str("%.3f" % (x2))),\
                    float(str("%.3f" % (y2))),\
                    float(str("%.3f" % (z2))),\
                    vr_lev
                    ])

    return outlist


### make field of "boxes"
boxes = make_boxspace()

### define database object and cursor object
box_data = lite.connect('boxes.db')
cur = box_data.cursor()

### write the list in memory to the database
cur.execute("DROP TABLE IF EXISTS boxes")
cur.execute("CREATE TABLE boxes(x1,y1,z1,x2,y2,z2,vr)")
cur.executemany("INSERT INTO boxes VALUES(?, ?, ?, ?, ?, ?, ?)", boxes)

### clear the 'boxes' list from memory
del boxes

### re-order the boxes
cur.execute("SELECT * FROM boxes ORDER BY z1 ASC")
cur.execute("SELECT * FROM boxes ORDER BY y1 ASC")
cur.execute("SELECT * FROM boxes ORDER BY x1 ASC")

### save the database
box_data.commit()

### print each item
while True:
    row = cur.fetchone()
    if row == None:
        break
    print(row)
Run Code Online (Sandbox Code Playgroud)

谢谢你们!!!

Dan*_*man 5

我真的不明白你在问什么,但我认为你对 SQL 有一些相当基本的误解。

SELECT... ORDER BY不“排序表”,并且commit在 SELECT 之后运行不会执行任何操作。使用不同的 ORDER BY 发送三个单独的 SELECT 但只运行fetch一次也没有任何意义:您只会获取最后一个 SELECT 提供的内容。

也许您只想一次按多列排序?

result = cur.execute("SELECT * FROM boxes ORDER BY z1, y1, x1 ASC")
rows = result.fetchall()
Run Code Online (Sandbox Code Playgroud)