迭代SQL数据库

时间:2014-01-16 16:45:27

标签: python sql sqlite

我是SQL的新手,我有一个应用程序,我想迭代一组格式为(p1,p2,id)的元组,并将当前元组与前一元组进行比较。

如果当前元组的p1与之前的p2匹配:

  • 将当前元组p1分配给上一个元组的p1
  • 上一个元组被删除

这是一个说明我的应用程序的例子:

import sqlite3 as lite

vectors = [
(1,2,1),
(2,3,2),
(3,4,3),
(4,5,4),
(6,7,5),
(7,8,6),
(8,9,7),
(9,10,8),
(11,12,9),
(12,13,10),
(13,14,11),
(15,16,12)
]

### define database object and cursor objects
vec_data = lite.connect('vecs.db')

cur = vec_data.cursor()
editor_cursor = vec_data.cursor()

### put python objects in SQL
cur.execute("DROP TABLE IF EXISTS vecs")
cur.execute("CREATE TABLE vecs(p1,p2,id INTEGER PRIMARY KEY)")
cur.executemany("INSERT INTO vecs VALUES(?, ?, ?)", vectors)

### iterate through and 'merge' vecs
cur.execute("SELECT * FROM vecs ORDER BY p1 ASC")
old_vec = None
while True:

    try:
        old_vec = vec
    except NameError:
        old_vec = None

    vec = cur.fetchone()
    if vec == None:
        break

    if (vec is not None) and (old_vec is not None):

        old_vec_p1 = old_vec[0]
        old_vec_p2 = old_vec[1]
        vec_p1 = vec[0]
        vec_id = vec[2]
        old_vec_id = old_vec[2]

        if (old_vec_p2 == vec_p1):
            editor_cursor.execute("UPDATE vecs SET p1=? WHERE id=?",(old_vec_p1,vec_id))
            editor_cursor.execute("DELETE FROM vecs WHERE id=?",(old_vec_id,))

cur.execute("SELECT * FROM vecs")       
while True:
    vec = cur.fetchone()
    if vec == None:
        break
    print(vec)

vec_data.close()

...现在给出了我的输入,我想要的输出是:

(1, 5, 4)
(6, 10, 8)
(11, 14, 11)
(15, 16, 12)

...但我得到了:

(3, 5, 4)
(8, 10, 8)
(12, 14, 11)
(15, 16, 12)

相当于我想要做的事情(但纯粹在python中完成)是:

vectors = [
[1,2,1],
[2,3,2],
[3,4,3],
[4,5,4],
[6,7,5],
[7,8,6],
[8,9,7],
[9,10,8],
[11,12,9],
[12,13,10],
[13,14,11],
[15,16,12]
]

i=0
while i < (len(vectors)-1):
    vec = (vectors[i+1])
    old_vec = (vectors[i])

    old_vec_p1 = old_vec[0]
    old_vec_p2 = old_vec[1]
    vec_p1 = vec[0]
    vec_id = vec[2]
    old_vec_id = old_vec[2]

    if (old_vec_p2 == vec_p1):
        vectors[i+1][0] = old_vec_p1
        del vectors[i]
    else:
        i += 1


for i in vectors:
    print i

所以我对SQL有误解或我的代码中有错误吗?

2 个答案:

答案 0 :(得分:1)

在您手中vec = cur.fetchone() vec后:您更新数据库中的行,但vec不会更改。

在数据库更改之后(或之前)添加vec = (old_vec_p1, vec[1], vec_id)

答案 1 :(得分:0)

    vecs = cur.execute("SELECT * FROM vecs ORDER BY p1 ASC")
    old_vec = None
    for vec in vecs:
        if vec and old_vec:
            old_vec_p1 = old_vec[0]
            old_vec_p2 = old_vec[1]
            vec_p1 = vec[0]
            vec_id = vec[2]
            old_vec_id = old_vec[2]
            if (old_vec_p2 == vec_p1):
                editor_cursor.execute("UPDATE vecs SET p1=? WHERE id=?",(old_vec_p1,vec_id))
                editor_cursor.execute("DELETE FROM vecs WHERE id=?",(old_vec_id,))
        old_vec = vec

    vecs = cur.execute("SELECT * FROM vecs")       
    print vecs.fetchall()
    vec_data.close()

看起来您的问题已经得到解答,其中vec未更新。我喜欢看不同的解决方案,所以我希望这可以帮到你。您可以简单地遍历返回的行。


    if vec and old_vec:

类似于

    if (vec is not None) and (old_vec is not None):