如何在SQLite3 python中通过从另一个表中选择参数来选择元素
是否可以返回城市列表,例如从波兰country_id=76返回,但在其中使用country.country行的country name值,而不是直接从country_id返回?数据库的结构如下如何在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_
@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。