使用 Python 和 SQLite3 从一个表中选择某些字段等于另一表中的行中的字段的行

发布于 2024-12-12 09:41:29 字数 1223 浏览 1 评论 0原文

我有一个小型数据库,它是一个几乎废弃的项目遗留下来的。该数据库有一个“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁梦 2024-12-19 09:41:29

我认为以下应该做你想要的:

...
c = conn.cursor()

c.execute('SELECT * FROM Pacientes')
for row in c.fetchall():
    Nome = row['nome']
    ID = row['Id']
    c.execute('SELECT File FROM Exames WHERE Id_Paciente=?', [ID])
    for exam in c:
        print exam['File']

I think the following should do what you want:

...
c = conn.cursor()

c.execute('SELECT * FROM Pacientes')
for row in c.fetchall():
    Nome = row['nome']
    ID = row['Id']
    c.execute('SELECT File FROM Exames WHERE Id_Paciente=?', [ID])
    for exam in c:
        print exam['File']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文