Python / SQLite-如何从所有列中进行选择并求和多个列?

时间:2019-03-01 07:34:10

标签: python sql sqlite

我尝试对SQLite数据库的搜索(选择)功能进行编码。当下,我从TextCtrl(这里简化为简短示例)获取了一个关键字,然后尝试在SQLite数据库的每一列中进行搜索。

keyword = "Product_1"

更好的是,如果我还可以选择多个关键字(列表):

keywords = ["Product_1", "Category_2"]

如何搜索所有列?

c.execute('SELECT * FROM Test WHERE AnyColumns=?', (keyword,))

稍后我需要总结两列。当时我是这样的:

c.execute("SELECT SUM(overall_cost) FROM vm WHERE id=5")
result = cur.fetchone()[0]
c.execute("SELECT SUM(overall_price) FROM vm WHERE id=5")
result2 = cur.fetchone()[0]

是否可以一步一步完成?像SUM(overall_cost) and SUM(overall_price)一样?

我需要将这两个值分开。不能合计为一个值!

1 个答案:

答案 0 :(得分:2)

可能

"SELECT SUM(overall_cost) as overall_cost,SUM(overall_price) as overall_price  FROM vm WHERE id=5"