Skip to content
Advertisement

sqlite database didn’t update table in my flask webhook python code

I try to update my sqlite database using flask webhook.

It seems commands line work fine if I type manually in the python console but my flask webhook didn’t update my SQLite database. It seems the apps fail at the “cursor.execute()” line.

here is my webhook code:

@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"}

Can you please tell me what’s wrong with my code ?

if it’s can help, here is my database structure (my db creation):

#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()

**I finally found the issue, webhooks need the full path to the SQLite database to work fine. I just start to code in python, it was a noob issue… **

Advertisement

Answer

I finally found the issue, webhooks need the full path to the SQLite database to work fine. I just start to code in python, it was a noob issue…

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement