gpt4 book ai didi

python - 如何在 Python 中比较两个 SQLite 数据集并搜索相似之处?

转载 作者:太空宇宙 更新时间:2023-11-03 21:48:02 25 4
gpt4 key购买 nike

基本上,我有两个数据库。一份为“学生”,一份为“老师”。教师数据库已保存在服务器上,学生数据库将通过用户输入创建。

这是给老师的(也单独保存在teacher.db文件中)

c.execute(“””CREATE TABLE teacher( 
ID varchar,
name text
age integer
interest1 varchar
interest2 varchar
)”””)
conn.commit()
conn.close()

示例:c.execute(“INSERT INTO Student VALUES (‘Lisa’, ‘35’, ‘spanish’, ‘productivity’)

这是学生的表,数据将保存在名为 Student.db 的单独文件中

c.execute(“””CREATE TABLE student( 
ID varchar,
name text
age integer
interest1 varchar
interest2 varchar
)”””)
conn.commit()
conn.close()

一个人的例子如下:

c.execute(“INSERT INTO student VALUES (‘Max’, ‘23’, ‘spanish’, ‘programming’)” 

现在,我想比较创建第二个数据库的用户输入。我们可以看到双方都使用了值“spanish”。现在我想创建一个像这样的算法:如果学生使用的关键字等于教师数据库中的数据集之一,我想打印出他们的名字。

我尝试使用 if then 但没有任何效果。

这是我学习 python 的第二天,请友善,即使这一切听起来很愚蠢。

最佳答案

我已经写了一个sql查询,我仍在考虑如何简化它,

SELECT name 
from student
where
student.interest1 in ((select interest1 from teacher) UNION (select interest2 from teacher))
or student.interest2 in ((select interest1 from teacher) UNION (select interest2 from teacher));

这应该可行,该查询通过检查学生的兴趣 1 或兴趣 2 是否与教师的任何一个兴趣匹配来工作。只需在游标内运行此命令,它就会返回一组名称。

关于python - 如何在 Python 中比较两个 SQLite 数据集并搜索相似之处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52332258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com