我有以下代码从SQLite数据库中选择行:
#!/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])
tuple1的示例:
[('tanulo1',), ('tanulo2',), ('tanulo3',), ... ('tanulo19',)]
其中tanulo19中的19的数字表示xlegnagySr = 19。
tuple2的示例:
[('munkdbnevesr1',), ('munkdbnevesr2',), ('munkdbnevesr3',),... ('munkdbnevesr13',)]
其中13表示ylegnagy=13
。
期望的结果应如下所示:
thirdtable = [('tanulo1','munkadbnevesr1'),('tanulo1','munkadbnevesr2'),...('tanulo1','munkadbnevesr13'),('tanulo2','munkadbnevesr1'),('tanulo2','munkadbnevesr2'),...,('tanulo2','munkadbnevesr13'), ...,('tanulo19','munkadbnevesr13')]
其中数字19和13表示xlegnagySr = 19且ylegnagy = 13.
所以我想要的是:在第三个数据库表中插入一些来自tuple1和tuple2的组合值。
我认为这样做的方法是将sql查询保存到tuple1和tuple2中,然后在for语句中将值插入到第三个表中。
做一些与此相反的事情:
# 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的例子是错误的,因为填充到第三个表中的所需数据,其名称为'egyedimunkadb'应该如下所示:
egyedimunkadb = [(1,'tanulo1',1),(2,'tanulo1',2),...(13,'tanulo1',13),(14,'tanulo2',1),(15,'tanulo2',2),...,(26,'tanulo2',13), ...,(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))
并使用我预期的数据填充第三个数据库表。
最好,Pál
答案 0 :(得分:1)
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';