Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python sqlite3使用两个参数进行选择_Python_Sql_Sqlite_Lambda - Fatal编程技术网

Python sqlite3使用两个参数进行选择

Python sqlite3使用两个参数进行选择,python,sql,sqlite,lambda,Python,Sql,Sqlite,Lambda,因此,我有一个具有两个属性的表,父属性和子属性。它们引用节点并创建表。我必须使用lambda函数,该函数仅当给定的两个参数共享同一个父参数时才返回父参数。例如,假设我有(父,子)对(a,b)和(a,e)。B和E是两个输入参数,因此由于它们共享一个父参数,func(B,E)将返回“a” import sqlite3 dog = sqlite3.connect("example1.db") c = dog.cursor() r = [('c','a'),('a','b'), ('a','e'),(

因此,我有一个具有两个属性的表,父属性和子属性。它们引用节点并创建表。我必须使用lambda函数,该函数仅当给定的两个参数共享同一个父参数时才返回父参数。例如,假设我有(父,子)对(a,b)和(a,e)。B和E是两个输入参数,因此由于它们共享一个父参数,func(B,E)将返回“a”

import sqlite3

dog = sqlite3.connect("example1.db")
c = dog.cursor()
r = [('c','a'),('a','b'), ('a','e'),('c','d'),
          ('c','f'),('f','g'), ('f','h'),('h','j'),
          ('h','i')]

c.execute('DROP TABLE IF EXISTS G')
c.execute('''CREATE TABLE G (Parent text, Child)''')

c.executemany('INSERT INTO G VALUES (?,?)', r)

dog.commit()


q = lambda x,y: 'SELECT G.Parent FROM G WHERE G.Child == "e"'


print(c.execute(q('b','e')).fetchone())
print(c.execute(q('h','g')).fetchone())
print(c.execute(q('e','a')).fetchone())

dog.close()
现在我有一个只返回“e”的父对象的对象,因为我迷路了。

应满足您查找公共父项要求的查询是:

由于您指定需要使用lambda函数,我猜这可能是某种类型的家庭作业。我不知道您或您的导师对lambda的限制是什么,但我可能会编写一个函数,返回一组父对象,然后使用lambda返回交叉点。伪代码,因为这可能是家庭作业:

def getParents:
    return set( parents of child )

q = lambda x,y: getParents(x).intersection(getParents(y))

print q('b','e')
def getParents:
    return set( parents of child )

q = lambda x,y: getParents(x).intersection(getParents(y))

print q('b','e')