Python 如何将所选行存储到元组中?
我有以下代码从SQLite数据库中选择行:Python 如何将所选行存储到元组中?,python,sqlite,pysqlite,Python,Sqlite,Pysqlite,我有以下代码从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 tanu
#!/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表示Y长度=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))
它用我所期望的数据填充第三个数据库表
最好的办法是,帕尔可以接受查询。在这种情况下,您需要所有可能的组合,即交叉连接:
插入egyedimunkadbtanulo_akie_amunkadb,munkadb_anevsorbol
选择塔努洛,索萨姆
来自塔努洛
交叉连接munkadbnevsora
其中tanulo.evf=?
和munkadbnevsora.evf=?
和tanulo.tannyelv='Sr';
这些价值应该如何组合?显示原始tuple1/tuple2值和所需结果的一些示例。我添加了原始tuple1/tuple2和所需结果的示例。我添加了数据库的SQL模式,我很抱歉,因为我刚刚意识到第三个数据库表中所需的数据看起来应该不同。自动增量值是自动生成的。我应该将上面的insert语句放入我的python代码中,因为表“tanulo”和“munkadbnevsora”是稍后填充数据的,而不是在创建数据库时填充的。因此,当我将数据插入前两个表中时,我应该运行python脚本将组合值插入第三个数据库表“egyedimunkadb”表中。对吗?当您想插入这些行时,应该执行INSERT。您的意思是不一定需要从我的python脚本执行INSERT,但可以在SQLite3 CLI中运行?