I am trying to send a get request to a backend route that returns some query results based on the parameters that are sent through the get request. At first I just used hardcoded values for my parameters that were passed to the backend route and the queries returned accurate results but when I replace those hardcoded parameters with the username that is a stateful object, it somehow reads the %s value as the column instead of the column name.
My get request looks like this:
async function getStatsData(username) {
const user = username;
const flashcard_id = 1;
const req = axios.get(`http://127.0.0.1:5000/stat/${user}/${flashcard_id}`)
const res = await req;
return res.data.results.map((statsItem, index) => {
return {
stat1: statsItem.stat1,
stat2: statsItem.stat2,
stat3: statsItem.stat3,
stat4: statsItem.user,
key: statsItem.key,
flashcard_id: statsItem.flashcard_id
}
})
}
and my backend route looks like this:
@app.route('/stat/<user>/<flashcard_id>', methods=['GET', 'POST', 'OPTIONS'])
def stats(user, flashcard_id):
def get_total_percent_correct(username):
correct = d.db_query('SELECT COUNT(*) FROM cards.responses WHERE guess = answer AND username = %s' % username)[0][0]
total = d.db_query('SELECT COUNT(*) FROM cards.responses WHERE username = %s' % username)[0][0]
try:
return round(float(correct)/float(total),3)*100
except:
print('0')
def get_percent_correct_for_flashcard(username,flashcard_id):
total = d.db_query('SELECT COUNT(*) FROM cards.responses WHERE username = %s AND flashcard_id = %s' % (username, flashcard_id))[0][0]
correct = d.db_query('SELECT COUNT(*) FROM cards.responses WHERE flashcard_id = %s AND guess = answer AND username = %s' % (flashcard_id, username))[0][0]
try:
return round(float(correct)/float(total),3)*100
except:
print('0')
def get_stats_for_flashcard(username, flashcard_id):
attempts = d.db_query('SELECT COUNT(*) FROM cards.responses WHERE username = %s AND flashcard_id = %s' % (username, flashcard_id))[0][0]
correct = d.db_query('SELECT COUNT(*) FROM cards.responses WHERE flashcard_id = %s AND guess = answer AND username = %s' % (flashcard_id, username))[0][0]
missed = attempts - correct
return attempts, correct, missed
data = [{
'flashcard_id': flashcard_id,
'user': user,
'stat1': get_total_percent_correct(user),
'stat2': get_percent_correct_for_flashcard(user, flashcard_id),
'stat3': get_stats_for_flashcard(user, flashcard_id),
'stat4': 'nothing yet',
'key':999
}]
return {"response_code" : 200, "results" : data}
The error is insided the function get_total_percent_correct. here is the full error message:
File "/home/bryant/work/memorylearning/my-app/backend/main.py", line 66, in get_total_percent_correct
correct = d.db_query('SELECT COUNT(*) FROM cards.responses WHERE guess = answer AND username = %s' % username)[0][0]
File "/home/bryant/work/memorylearning/my-app/backend/main.py", line 17, in db_query
self.cursor.execute(q)
psycopg2.errors.UndefinedColumn: column "beans" does not exist
LINE 1: ...OM cards.responses WHERE guess = answer AND username = beans
I appreciate any help. Thanks for your time
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.