通过python sqlite数据库迭代

时间:2014-01-13 13:59:32

标签: 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)

谢谢你们!

2 个答案:

答案 0 :(得分:4)

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

SELECT... ORDER BY没有“命令表”,并且在SELECT没有做任何事情后运行commit。使用不同的ORDER BY发送三个单独的SELECT但仅运行fetch一次也没有任何意义:您只需获取最后一个SELECT提供的内容。

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

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

答案 1 :(得分:0)

我认为连接到sqlite3数据库就像我们所知道的那样简单。 因为我们直接从数据库访问数据库查询和结果,所以我们需要使用fechall()方法将所有结果放在列表中并迭代抛出该列表。这样您就可以通过单个连接在多个列表中获得任意数量的结果。 以下是简单的python代码

conn = sqlite3.connect("database file name")
cur = conn.cursor()
cur.execute("your query")
a_list = cur.getchall()
for i in a_list:
    "process your list"
"perform another operation using cursor object"