![](/img/trans.png)
[英]SQLITE3 DB data transfer from one field to another in the same table and same row
[英]Select rows from one table where certain field is equal to a field in a row from another table, with Python and SQLite3
我有一个小型数据库,该数据库是一个几乎不存在的项目所遗留下来的。 该数据库具有一个“患者”表和一个“考试”表,其中每个患者数据都有一个个人数据和一个唯一的“ Id”字段,每个表的“考试”表都有一些字段,其中一个字段是“ Patient_Id”。
我想要的是,对于给定的患者(“ Pacientes”表中的行),“ Exames”表中的“ 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']
===========================
一个类似问题的答案似乎是我想要的,但我不知道如何在带sqlite3
模块的Python中执行此操作,更不用说此表达式中的基本内容和附带内容,或语法结构是什么:
SELECT i.prof_image
FROM profile_images i
WHERE cat_id = (select max(cat_id) from images_cat)
我认为以下应该做您想要的:
...
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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.