How to store selected rows into tuple?

Pal Csanyi

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','munkad‌​bnevesr13'),('tanulo‌​2','munkadbnevesr1')‌​,('tanulo2','munkadb‌​nevesr2'),...,('tanu‌​lo2','munkadbnevesr1‌​3'), ...,('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,'tanulo‌​2',1)‌​,(15,'tanulo2',2),...,(26,'tanu‌​lo2',1‌​3), ...,(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

CL.

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';

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to store values of selected columns in separate rows?

How to store in a JavaScript variable the selected rows in a table

Store selected rows id

store selected rows on PFQueryTableViewController

how can i store selected rows of tableview in nsuserdefaults in swift 3

How to update the selected rows?

How to store last selected button

How to store selected avatar into database

How to store the selected value in a variable?

How to store selected information in array?

How to delete rows with selected text?

How to delete selected rows in uitable?

How to reset selected rows in Shiny

How to calculate the average of rows/selected rows in the matrix

How store a tuple for NSGradient(colorsAndLocations:) in swift

how to store tuple value as python variable

How to select tuple from rows in oracle

Set Unique ID for selected rows in a table to store in localStorage

How to store selected Ringtone in android studio?

How to store selected items of an ExpandableListView in an ArrayList

How validate if a specific value was selected and store it in Laravel

How to store location of selected video from PhotoLibrary

How to store the ID of selected dropdown value in SQLAlchemy

How to store selected datepicker javascript using PHP

Spring JdbcTemplate: how to limit selected rows?

How to compare cells of one column for selected rows?

How to assign a vector of values to rows selected with the %in% operator

How to transform selected rows into a single column in R

How to reset ant design table selected rows?