Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQLite3 python中通过从另一个表中选择参数来选择元素_Python_Python 3.x_Sqlite_Flask - Fatal编程技术网

如何在SQLite3 python中通过从另一个表中选择参数来选择元素

如何在SQLite3 python中通过从另一个表中选择参数来选择元素,python,python-3.x,sqlite,flask,Python,Python 3.x,Sqlite,Flask,是否可以返回城市列表,例如从波兰country_id=76返回,但在其中使用country.country行的country name值,而不是直接从country_id返回?数据库的结构如下 @app.route('/citiess') def city_list3(): db = get_db() data = db.execute(''' SELECT city FROM city JOIN country ON city.country_

是否可以返回城市列表,例如从波兰country_id=76返回,但在其中使用country.country行的country name值,而不是直接从country_id返回?数据库的结构如下

@app.route('/citiess')
def city_list3():
    db = get_db()
    data = db.execute('''
        SELECT city FROM city 
        JOIN country ON city.country_id = country.country_id
        WHERE city.country_id = 76
        ''').fetchall()
    data_json = []
    for i in data:
        data_json.extend(list(i))

    return jsonify(data_json)

为什么不在连接的国家/地区表上执行WHERE子句,即:

data = db.execute("""SELECT city FROM city
                     JOIN country USING (country_id)
                     WHERE country.country = ?""", ("Poland", )).fetchall()

我认为这也行得通:

data = db.execute('''
    SELECT city FROM city 
    JOIN country ON country.country_id = city.country_id
    WHERE country.country = "Poland"
    ''').fetchall()

无论如何,谢谢

我只是将其参数化了,这样您就可以安全地将查询重新用于您想要的任何国家,而不是直接更改SQL。