简体   繁体   English

python sqlite3 执行多个 select 查询

[英]python sqlite3 execute multiple select queries

I have a table called profil parent table of academic and projet and am tryin to loop through all profils and get their academic and projet我有一个名为profil parent table of Academicprojet的表,我正在尝试遍历所有配置文件并获取他们的 Academic 和 projet

import pandas as pd
import sqlite3

conn = sqlite3.connect("Desktop/Dev/ScoutIT/src/db.sqlite3")
cur = conn.cursor()
candidats_id = cur.execute("select id from Candidat_profil ;")
for candidat_id in candidats_id: 
    academic= cur.execute('select * from Candidat_academic where profil_id = ?' , candidat_id)
    projet= cur.execute('select * from Candidat_projet_realise where profil_id = ?' , candidat_id)
    for row in academic:
        print(row)
    for row in projet:
        print(row)

the code above only print out projet but when i comment out projet i get academic printed.上面的代码只打印出 projet 但是当我注释掉 projet 时,我得到了学术打印。

Note: i have more child tables i want to implement in this code注意:我有更多要在此代码中实现的子表

try to chagne your code to the following:尝试将您的代码更改为以下内容:

import pandas as pd
import sqlite3    
conn = sqlite3.connect("Desktop/Dev/ScoutIT/src/db.sqlite3")
cur = conn.cursor()
candidats_id = cur.execute("select id from Candidat_profil ;")
for candidat_id in candidats_id: 
    cur.execute('select * from Candidat_academic where profil_id = ?' , candidat_id)
    academic=cur.fetchall()
    cur.execute('select * from Candidat_projet_realise where profil_id = ?' , candidat_id)
    projet= cur.fetchall()
    for row in academic:
        print(row)
    for row in projet:
        print(row)

I advice you to use cur.fetchone() or cur.fetchall() for the documentation, feel free to visit: a我建议您使用cur.fetchone()cur.fetchall()获取文档,请随时访问: a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM