【问题标题】:Using request body in SELECT statement在 SELECT 语句中使用请求正文
【发布时间】:2020-05-11 11:10:23
【问题描述】:

我正在寻找一种方法来使用我的 Twilio 编号中的 request.values.get 的主体,以便在 SQLite 中的 SELECT 语句中使用。

基本上,如果我的 Twilio 号码的短信正文是“ABC123”,我想在我的选择声明中使用该文本... Rego='ABC123'。显然“ABC123”是动态的,并且会根据用户输入一直变化。希望这是有道理的,我对 SQLite 和 Python 都很陌生。提前致谢 这是我的代码!!

from flask import Flask, request, redirect
from twilio.twiml.messaging_response import MessagingResponse

app = Flask(__name__)

@app.route("/sms", methods=['GET', 'POST'])
def incoming_sms():

    # Get the message body sent to my Twilio number
    body = request.values.get('Body', None)

#Below carries out the DB lookup based on body sent to my Twilio number
import sqlite3 


conn = sqlite3.connect('VinLookup.db')
c = conn.cursor()


def read_from_db():
    c.execute("SELECT Vin FROM Vin_Data WHERE Rego='ABC123'")
    data = c.fetchall()
    print(data)


read_from_db()   
c.close()
conn.close()

【问题讨论】:

    标签: python sqlite flask twilio


    【解决方案1】:

    也许是这样的。所有数据库内容现在都在 read_from_db 函数中,该函数接受 rego 参数作为输入。

    import sqlite3
    from flask import Flask, request, redirect
    from twilio.twiml.messaging_response import MessagingResponse
    
    
    def read_from_db(rego):
        with sqlite3.connect("VinLookup.db") as conn:
            c = conn.cursor()
            c.execute("SELECT Vin FROM Vin_Data WHERE Rego=?", (rego,))
            return c.fetchall()
    
    
    app = Flask(__name__)
    
    
    @app.route("/sms", methods=["GET", "POST"])
    def incoming_sms():
        body = request.values.get("Body", "")
        result = read_from_db(body)
        print(body, result)
        # Presumably return something here? A response to the SMS?
    

    【讨论】:

    • 非常感谢您的帮助。所以在回复短信时,我会使用 #body= request.values.POST("Body", "")
    猜你喜欢
    • 1970-01-01
    • 2020-04-25
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 2011-09-19
    • 2016-05-02
    • 1970-01-01
    相关资源
    最近更新 更多