【发布时间】:2018-04-17 08:18:51
【问题描述】:
我有以下代码从 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])
元组 1 的示例:
[('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))
它按照我的预期用数据填充第三个数据库表。
最好的,帕尔
【问题讨论】:
-
应该如何组合这些值?显示原始
tuple1/tuple2值和所需结果的一些示例。 -
我添加了原始 tuple1/tuple2 的示例和所需的结果。