【问题标题】:How to store selected rows into tuple?如何将选定的行存储到元组中?
【发布时间】: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','munkad‌​bnevesr13'),('tanulo‌​2','munkadbnevesr1')‌​,('tanulo2','munkadb‌​nevesr2'),...,('tanu‌​lo2','munkadbnevesr1‌​3'), ...,('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,'tanulo‌​2',1)‌​,(15,'tanulo2',2),...,(26,'tanu‌​lo2',1‌​3), ...,(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 的示例和所需的结果。

标签: python sqlite pysqlite


【解决方案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';

【讨论】:

  • 我添加了我的数据库的 SQL 模式,我很抱歉,因为我刚刚意识到第三个数据库表中所需的数据应该看起来不同。
  • 自增值是自动生成的。
  • 我应该将上面的插入语句放入我的 python 代码中,因为表 'tanulo' 和 'munkadbnevsora' 稍后会填充数据,而不是在创建数据库时。因此,当我将数据插入到前两个表中时,我应该运行我的 python 脚本将组合值插入到第三个数据库表中,即“egyedimunkadb”表中。对吗?
  • 当您想要插入这些行时,您应该执行 INSERT。
  • 你的意思是不需要从我的python脚本中执行INSERT,而是可以在SQLite3 CLI中运行?
猜你喜欢
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 1970-01-01
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多