I'm trying to get this to work and the error message 'line 49, in pushNumber cursor.execute(query_string, latestNumber) sqlite3.OperationalError: row value misused' keeps coming up. As well as that, when using the pop function this comes up: 'sqlite3.OperationalError: table Stack has 1 columns but 3 values were supplied'
I'm trying to get it so that when I visit the /calc/push/50 webpage, it adds 50 to a list and then creates a table in the Stack database. When I enter /calc/push/8, it then appends 8 onto the list and inserts this into the database. One column is supposed to just show the full list, getting updated each time with push, and the other column is supposed to show all of the logs of whatever function I've used with a new record inserted underneath the last each time.
from flask import Flask, render_template
import sqlite3
from flask.wrappers import Request
app = Flask(__name__)
conn = sqlite3.connect('stack.db')
cursor = conn.cursor()
stackList = []
latestNumber = 0 #For pop/peek function
columnCreated = 0
#cursor.execute("""CREATE TABLE Stack (
# StackLogs text
# Stacklist text
# )""")
#conn.commit()
@app.route('/')
@app.route('/calc/')
@app.route('/calc/peek')
def calcPeek():
return render_template('peek.html', latestNumber=latestNumber) #Prints top of stack - recent number. Could also use [-1]
@app.route('/calc/push/<number>') #<> passes number as a variable
def pushNumber(number):
stackList.append(number)
global latestNumber
latestNumber = number #Assigns most recent number to this variable
print(stackList) #Prints to console so we can check the list
global columnCreated
columnCreated += 1
print(columnCreated)
if columnCreated == 1:
conn = sqlite3.connect('stack.db')
cursor = conn.cursor()
stack_string = ', '.join('?' * len(stackList))
query_string = 'INSERT INTO Stack VALUES (%s);' % stack_string
cursor.execute(query_string, stackList)
conn.commit()
conn.close()
elif columnCreated >= 2:
conn = sqlite3.connect('stack.db')
cursor = conn.cursor()
stack_string = ', '.join('?' * len(latestNumber))
query_string = 'UPDATE Stack SET StackLogs=(%s);' % stack_string
cursor.execute(query_string, latestNumber)
conn.commit()
conn.close()
return render_template('push.html', number=number, latestNumber=latestNumber)
@app.route('/calc/pop')
def calcPop():
global latestNumber
if stackList == []: #If empty, prints to console. Otherwise pops end off of list.
print("Stack is empty")
else:
stackList.pop() #Removes last value by default
conn = sqlite3.connect('stack.db')
cursor = conn.cursor()
stack_string = ', '.join('?' * len(stackList))
query_string = 'INSERT INTO Stack VALUES (%s);' % stack_string
cursor.execute(query_string, stackList)
conn.commit()
conn.close()
return render_template('pop.html', stackList=stackList, latestNumber=latestNumber)