Reputation: 25314
#user_input.py
from flask import Flask, request, jsonify
import sqlite3
app = Flask(__name__)
def process_user_input(data):
# Retrieve user input from the data
stock_name = data.get('stockName')
# Create a new connection and cursor
conn = sqlite3.connect('C:/SQLite/pythonstock/database.db')
c = conn.cursor()
c.execute("SELECT price FROM stocks WHERE lower(stockName) = ?", (stock_name.lower(),))
# Fetch the result
row = c.fetchone()
if row:
price = row[0] # Access the first column (price)
# Perform further operations with the retrieved price
else:
# Handle the case when no row is found for the given stock_name
price = None # or any appropriate default value or error handling
# Close the cursor and connection
c.close()
conn.close()
processed_input = price
# Create a JSON-serializable response
response = {'processed_input': processed_input}
return response
def process_user_input1(data):
print("Process_user_input1 started")
# Retrieve user input from the data
stock_name = data.get('stockName')
BRstage=data.get('BRstage')
# Create a new connection and cursor
conn = sqlite3.connect('C:/SQLite/pythonstock/database.db')
c = conn.cursor()
c.execute('update stocks set BRstage=BRstage where stockName=stock_name')
conn.commit()
conn.close()
processed_input1='success'
response = {'processed_input1': processed_input1}
return response
Traceback (most recent call last):
File "C:\Python312\Lib\site-packages\flask\app.py", line 1455, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\flask\app.py", line 869, in full_dispatch_request
rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\flask_cors\extension.py", line 176, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\flask\app.py", line 867, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\flask\app.py", line 852, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\Steven\stock.investing.decision.making\my-app\flask-app\main.py", line 25, in process_user_input_route1
result = process_user_input1(data)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\Steven\stock.investing.decision.making\my-app\flask-app\user_input.py", line 57, in process_user_input1
c.execute('UPDATE stocks SET BRstage = ? WHERE lower(stockName) = ?', (BRstage, stock_name.lower()))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: no such column: stock_name
127.0.0.1 - - [06/Nov/2023 22:11:41] "POST /process_user_input1 HTTP/1.1" 500 -
Upvotes: 0
Views: 46
Reputation: 6520
Fix the problem in this line c.execute('update stocks set BRstage=BRstage where stockName=stock_name')
. It needs to use parameter substitution (bound variable) for stock_name
because as written it is looking for a column named stock_name
. Or maybe it's an incomplete picture of what is going on. The traceback complains about line 57. There are at most (including blank lines) 56 lines of posted code.
Upvotes: 1