如何将选定的行存储到元组中?

How to store selected rows into tuple?

我对来自 SQLite 数据库的 select 行有以下代码:

#!/usr/bin/python3.4
# -*- coding: utf-8 -*-

import sqlite3 as lite
import sys

csat = lite.connect('Tanuloim.db')

with csat:
    hely = csat.cursor()

    for evflym in range (5, 6):
    hely.execute('select count() from tanulo where evf=? and tannyelv="Sr"', (evflym,))
    xlegnagySr = (hely.fetchone()[0])

    hely.execute('select count() from tanulo where evf=? and tannyelv="Hu"', (evflym,))
    xlegnagyHu = (hely.fetchone()[0])


    hely.execute('select count() from munkadbnevsora where evf=?', (evflym,))
    ylegnagy = (hely.fetchone()[0])

    print ('Ennyi magyar ötödikes tanuló van - xlegnagyHu:', xlegnagyHu)
    print ('Ennyi szerb ötödikes tanuló van - xlegnagySr:', xlegnagySr)
    print ('Ennyi munkadarab van az ötödikben - ylegnagy:', ylegnagy)
    print ('evfolyam:', evflym)

    hely.execute('select tanuloneve from tanulo where evf=? and tannyelv="Sr"', (evflym,))
    """" This is returned as a tuple, let it named tuple1. """
    for x in range (0, xlegnagySr):
        print (hely.fetchone()[0])

    hely.execute('select munkdbnevesr from munkadbnevsora where evf=?', (evflym,)')
    """" This is returned as a tuple, let it named tuple2. """
    for y in range (0, ylegnagy):
        print (hely.fetchone()[0])

元组 1 的示例:

[('tanulo1',), ('tanulo2',), ('tanulo3',), ... ('tanulo19',)] 

其中 tanulo19 中的 19 表示 xlegnagySr=19。

元组 2 的示例:

[('munkdbnevesr1',), ('munkdbnevesr2',), ('munkdbnevesr3',),... ('munkdbnevesr13',)] 

其中 13 表示 ylegnagy=13

所需的结果应如下所示:

thirdtable = [('tanulo1','munkadbnevesr1'),('tanulo1','munkadbnevesr2'),.‌​..('tanulo1','munkad‌​bnevesr13'),('tanulo‌​2','munkadbnevesr1')‌​,('tanulo2','munkadb‌​nevesr2'),...,('tanu‌​lo2','munkadbnevesr1‌​3'), ...,('tanulo19','munkadbnevesr13')]  

其中数字 19 和 13 表示 xlegnagySr=19 和 ylegnagy=13。

所以我想要的是:将 tuple1 和 tuple2 的一些组合值插入第三个数据库 table。

我认为这样做的方法是将 sql 查询保存到 tuple1 和 tuple2 中,然后在 for 语句中将值插入第三个 table.

做与此相反的事情:

# Larger example that inserts many records at a time
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
             ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
             ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
            ]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)

我该怎么做?

我的数据库的架构如下:

CREATE TABLE munkadbnevsora (
  sorszam integer CONSTRAINT ek_munkadbnevsora PRIMARY KEY AUTOINCREMENT ,
  evf integer NOT NULL ,
  negyedev integer NOT NULL ,
  munkdbnevehu text NOT NULL COLLATE nocase ,
  munkdbnevesr text NOT NULL COLLATE nocase
);
CREATE TABLE tanulo (
  az integer CONSTRAINT ek_tanulo PRIMARY KEY AUTOINCREMENT ,
  azszam integer UNIQUE NOT NULL ,
  tanuloneve text NOT NULL COLLATE nocase ,
  tannyelv text NOT NULL COLLATE nocase ,
  evf integer NOT NULL ,
  tagozat text NOT NULL COLLATE nocase ,
  osztfonok text NOT NULL COLLATE nocase 
);
CREATE TABLE egyedimunkadb (
  az integer CONSTRAINT ek_egyedimunkadb PRIMARY KEY AUTOINCREMENT,
  tanulo_akie_amunkadb text NOT NULL REFERENCES tanulo (azszam) ON DELETE CASCADE ON UPDATE CASCADE ,
  munkadb_anevsorbol integer NOT NULL REFERENCES munkadbnevsora (sorszam) ON DELETE CASCADE ON UPDATE CASCADE ,
  jegy integer ,
  indoklas text
);

所以我意识到我的 python 代码和 tuple2 示例是错误的,因为所需的数据填充到第三个 table,其名称为 'egyedimunkadb' 应该是这样的:

egyedimunkadb = [(1,'tanulo1',1),(2,'tanulo1',2),.‌​..(13,'tanulo1',13),(14,'tanulo‌​2',1)‌​,(15,'tanulo2',2),...,(26,'tanu‌​lo2',1‌​3), ...,(N,'tanulo19',19)]

其中 N 是我现在不知道的数字。

终于成功了! 我放了@CL的以下代码。进入 python 脚本:

hely.execute('INSERT INTO egyedimunkadb(tanulo_akie_amunkadb, munkadb_anevsorbol) SELECT tanuloneve, sorszam FROM tanulo CROSS JOIN munkadbnevsora WHERE tanulo.evf = ? AND munkadbnevsora.evf = ? AND tanulo.tannyelv = "Sr" ', (evflym, evflym))

它会按照我的预期使用数据填充第三个数据库 table。

最好的,Pál

INSERT statement可以接受查询。在这种情况下,你想要所有可能的组合,这是一个交叉连接:

INSERT INTO egyedimunkadb(tanulo_akie_amunkadb, munkadb_anevsorbol)
SELECT tanulo, sorszam
FROM tanulo
CROSS JOIN munkadbnevsora
WHERE tanulo.evf = ?
  AND munkadbnevsora.evf = ?
  AND tanulo.tannyelv = 'Sr';