【问题标题】:sqlite database didn't update table in my flask webhook python codesqlite 数据库没有更新我的烧瓶 webhook python 代码中的表
【发布时间】:2021-10-27 23:01:02
【问题描述】:

我尝试使用flask webhook 更新我的sqlite 数据库。

如果我在 python 控制台中手动输入,但我的烧瓶 webhook 没有更新我的 SQLite 数据库,似乎命令行工作正常。似乎应用程序在“cursor.execute()”行失败了。

这是我的 webhook 代码:

@app.route('/trendanalyser', methods=['POST'])
def trendanalyser():
    data = json.loads(request.data)
    if data['passphrase'] == config.WEBHOOK_PASSPHRASE:
#Init update variables
        tastate = data['TrendAnalyser']
        date_format = datetime.today()
        date_update = date_format.strftime("%d/%m/%Y %H:%M:%S")
        update_data = ((tastate), (date_update))
#Database connection
        connection = sqlite3.connect('TAState15min.db')
        cursor = connection.cursor()
#Database Update
        update_query = """Update TrendAnalyser set state = ?, date = ? where id = 1"""
        cursor.execute(update_query, update_data)
        connection.commit()
        return("Record Updated successfully")
        cursor.close()
    else:
        return {"invalide passphrase"}

你能告诉我我的代码有什么问题吗?

如果有帮助,这是我的数据库结构(我的数据库创建):

#Database connection
conn = sqlite3.connect("TAState15min.db")
cursor = conn.cursor()
#Create table
sql_query = """ CREATE TABLE TrendAnalyser (
    id integer PRIMARY KEY,
    state text,
    date text
)"""
cursor.execute(sql_query)
#Create empty row with ID at 1
insert_query = """INSERT INTO TrendAnalyser
                          (id, state, date)
                          VALUES (1, 'Null', 'Null');"""
cursor.execute(insert_query)
conn.commit()
#Close database connexion
cursor.close()

**我终于找到了问题,webhooks 需要 SQLite 数据库的完整路径才能正常工作。我刚开始用 python 编码,这是一个菜鸟问题...... **

【问题讨论】:

  • “似乎应用程序在“cursor.execute()”行失败了。”,你收到错误了吗?如果是这样,错误是什么
  • 我假设错误在这一行,通过使用返回命令进行测试。都在此行通过之前返回,但之后不返回。 webhook 发送的错误返回是:“500 Internal server error”
  • 不要在问题中添加解决方案。请查看Can I answer my own question?。另请参阅:Accept Your Own Answers.
  • 谢谢。我是论坛的新人,正在寻找,但第一眼没有找到。我现在以正确的方式结束我的问题。

标签: python sqlite flask


【解决方案1】:

我终于找到了问题,webhook 需要 SQLite 数据库的完整路径才能正常工作。我刚开始用python编写代码,这是一个菜鸟问题...

【讨论】:

  • 为了帮助其他有类似问题的人,也许发布为您解决问题的更改代码。
猜你喜欢
  • 2017-01-15
  • 2021-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 1970-01-01
  • 2019-02-24
  • 1970-01-01
相关资源
最近更新 更多