在SELECT语句中使用请求正文

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

基本上,如果发送到我的Twilio号码的SMS正文为“ ABC123”,我想在我选择的STATEMENT ... Rego ='ABC123'中使用该TEXT。显然,“ 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()
评论
  • alber1986
    alber1986 回复

    Something like this, perhaps. All of the database stuff is now in the read_from_db function, which accepts the rego parameter as input.

    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?