【问题标题】:SQLIite column name has trailing parenthesisSQLite 列名有尾括号
【发布时间】:2018-12-12 12:29:27
【问题描述】:

当我查询我的 SQLite 数据库时,返回的列名之一有一个尾括号。数据库表架构为:

CREATE TABLE 'transactions' ('user_id' bigint, 'symbol' text,'shares' integer, 'date' datetime)

我正在查询登录用户拥有的股票。我正在使用SUM(transactions.shares)GROUP BY transactions.symbol,以便将多个条目中的股票数量相加,并按特定股票代码进行组织。

stocks_owned = db.execute("SELECT transactions.symbol, SUM(transactions.shares) FROM transactions WHERE user_id=:id GROUP BY transactions.symbol", id=session["user_id"])

print(stocks_owned) 显示:

[{'symbol': 'ABC', 'shares)': 17}, {'symbol': 'ABT', 'shares)': 6}, {'symbol': 'BAB', 'shares)': 12}, {'symbol': 'BBC', 'shares)': 16}, {'symbol': 'BBN', 'shares)': 6}, {'symbol': 'DAX', 'shares)': 2}, {'symbol': 'SEA', 'shares)': 12}, {'symbol': 'SEE', 'shares)': 12}]

shares 键中有一个括号:'shares)',它会导致访问模板中的值出现问题。为什么有这个括号,我该如何修复我的查询,所以它不是?

【问题讨论】:

  • @Car - 对,但问题是,这个错字是从哪里来的? stock_owned 是由 db.execute(...) 创建的
  • 啊抱歉!浏览得太快了。由于查询正在返回共享数据,听起来db.execute 存在解析问题。您可以通过进入数据库并查看表名来验证这一点,并验证shares 中没有流氓),然后深入到库的db.execute 函数以查看) 的位置in(可能最容易使用调试器遍历它)。

标签: python sqlite


【解决方案1】:

)(不知何故:怀疑 dbname 前缀中的 .)来自 SUM(transaction.shares) 中的右括号。为聚合添加一个列别名,即SUM(transaction.shares) shares

【讨论】:

  • 哇,这解决了问题!谢谢你。当我执行“SUM(transactions.shares) A​​S 共享”时,它现在可以在有或没有“AS”的情况下工作
猜你喜欢
  • 1970-01-01
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多