I have following code to select rows from a SQLite database:
#!/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])
Example for tuple1:
[('tanulo1',), ('tanulo2',), ('tanulo3',), ... ('tanulo19',)]
where the number of 19 in tanulo19 indicate that that xlegnagySr=19.
Example for tuple2:
[('munkdbnevesr1',), ('munkdbnevesr2',), ('munkdbnevesr3',),... ('munkdbnevesr13',)]
where 13 indicate that that ylegnagy=13
.
The desired result should look like this:
thirdtable = [('tanulo1','munkadbnevesr1'),('tanulo1','munkadbnevesr2'),...('tanulo1','munkadbnevesr13'),('tanulo2','munkadbnevesr1'),('tanulo2','munkadbnevesr2'),...,('tanulo2','munkadbnevesr13'), ...,('tanulo19','munkadbnevesr13')]
where numbers 19 and 13 indicates that that xlegnagySr=19 and ylegnagy=13.
So what I want is this: to insert into third database table some combined values from tuple1 and tuple2.
I think the way for this is to save sql queries into tuple1 and tuple2 and then in a for statement insert values into third table.
To do something that is opposite to this:
# 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)
How can I do this?
The schema for my database follows:
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
);
So I realized that that my python code and example of tuple2 is wrong, because the desired data filled into third table, which has the name 'egyedimunkadb' should look like this:
egyedimunkadb = [(1,'tanulo1',1),(2,'tanulo1',2),...(13,'tanulo1',13),(14,'tanulo2',1),(15,'tanulo2',2),...,(26,'tanulo2',13), ...,(N,'tanulo19',19)]
where N is a number I do not know now.
Finally it works! I put the following code of @CL. into python script:
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))
and it populates the third database table with data as I expected.
Best, Pál
The INSERT statement can accept a query. In this case, you want all possible combinations, which is a cross join:
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';