使用 Python 和 SQLite3 从一个表中选择某些字段等于另一表中的行中的字段的行
我有一个小型数据库,它是一个几乎废弃的项目遗留下来的。该数据库有一个“Patents”表,其中包含个人数据和唯一的“Id”字段,以及一个“Exams”表,其中包含每次检查的一些字段,其中一个字段是“Patient_Id”。
我想要的是,对于给定的患者(“Pacientes”表中的行),“Exames”表中的“Patient_Id”与给定患者的“Patient_Id”匹配的检查(行)。
我是 SQL 的初学者,所以如果问题很幼稚,我深表歉意。
我的工作代码如下,但我使用的是 for 循环,而我更愿意有一个 SQL 查询(我认为这就是使用数据库的要点......):
#!/usr/bin/env python
# coding: utf-8
import os, sqlite3
conn = sqlite3.connect('BDdata.db3')
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute('SELECT * FROM Exames')
exams = c.fetchall()
c.execute('SELECT * FROM Pacientes')
for row in c:
Nome = row['nome']
ID = row['Id']
for exam in exams: # I would like to replace this loop
if exam['Id_Paciente'] == ID: # with a SQL query meaning
print exam['File']
========== ==================
类似问题的答案似乎是我想要的,但我不知道如何在Python中使用sqlite3来做到这一点> 模块,更不用说这个表达式中什么是必需的,什么是附带的,或者语法结构是什么:
SELECT i.prof_image
FROM profile_images i
WHERE cat_id = (select max(cat_id) from images_cat)
I have a small database which is legacy from an almost defunct project. This database has a "Patients" table with individual personal data and an unique "Id" field, and an "Exams" table with some fields for each exam, one of these fields being "Patient_Id".
What I want is, for a given patient (row from "Pacientes" table) the exams (rows) from "Exames" table whose "Patient_Id" matches that of the given patient.
I am very beginner with SQL, so if the question is very naive, I apologize.
My working code is the following, but I am using a for loop while I would much rather have a SQL query (which is the very point of using databases, I think...):
#!/usr/bin/env python
# coding: utf-8
import os, sqlite3
conn = sqlite3.connect('BDdata.db3')
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute('SELECT * FROM Exames')
exams = c.fetchall()
c.execute('SELECT * FROM Pacientes')
for row in c:
Nome = row['nome']
ID = row['Id']
for exam in exams: # I would like to replace this loop
if exam['Id_Paciente'] == ID: # with a SQL query meaning
print exam['File']
============================
An answer to a similar question seems to be what I want, but I have no idea how to do this in Python with sqlite3
module, much less what in this expression is essential and what is incidental, or what is the syntax structure:
Selecting rows from a table by One a field from other table
SELECT i.prof_image
FROM profile_images i
WHERE cat_id = (select max(cat_id) from images_cat)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为以下应该做你想要的:
I think the following should do what you want: