sqlite3.Cursor object has no attribute __getitem__ Error in Python Flask

2024/11/17 3:49:39

This is my code. I get this error everytime I press login:

'sqlite3.Cursor' object has no attribute '__getitem__'

This is my login tab:

@app.route('/', methods=['GET', 'POST'])
def login():error= Noneif request.method == "POST":with sql.connect("database.db") as con:cur = con.cursor()try:data = cur.execute("SELECT name FROM users WHERE name= ?",(request.form['username'],) )data.fetchone()if sha256_crypt.verify(request.form['password'], data[0]):session['logged_in'] == Truesession['username'] = request.form('username')flash('Successfully logged in')return redirect(url_for(hello_world))else:error = "Invalid Password or User. Try again."except Exception as e:flash(e)return render_template('login.html',error=error)con.close()gc.collect()return render_template('login.html', error=error)

What I am doing wrong?

Answer

__getitem__ refers to square bracket access, in this case data[0]. The error tells you that you can't use cursors like that. Replace data[0] with the value returned from data.fetchone().

https://en.xdnf.cn/q/118851.html

Related Q&A

Merge Sort Implementation Check

I am doubtful of my implementation of the merge sort for two cases specifically:1. If the size of the list is 2, then I have swapped the values if they are not in the ascending order else I have return…

How to create a def in python that pick a specific value and then make a new dict like this

myDict ={"key1" : "val1","key2" : "val2","key3" : "val3","key4" : "x","key5" : "x"}I need a def in py…

Inputs required in python on csv files

I have a problem and need to solve it using Pandas/Python. Not sure how to achieve it and would be great if someone help here to build the logic. I have to generate the output file as below: df = pd.Da…

ServiceBusError : Handler failed: tuple object has no attribute get_token

Im getting the below error when i run my code. This code is to requeue the Deadletter messages. Error: Exception has occurred: ServiceBusError Handler failed: tuple object has no attribute get_token. A…

sqlite3.OperationalError: near WHERE: syntax error

I want to update a series of columns Country1, Country2... Country 9 based on a comma delimited string of country names in column Country. Ive programmed a single statement to accomplish this task. cur…

If statement not working correctly in Python 3

This is the start of an RPG I am going to make, and It runs smoothly until I try to change the gender by saying yes or any other of the answers that activate the if statement. Is there something I am f…

pymc3 error. AttributeError: module arviz has no attribute geweke [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

how to prevent duplicate text in the output file while using for loop

I have this code which compares a number to a number(what i called item in my code) in the domain range to see if it is already there. If it its then print to the output file if it is not then only pri…

How to replace \\ with \ without raising an EOL error?

I am reading from a file that contains byte data but when I open the file and store the readline data into a variable it stores it in a string with backslash escapes, So when trying to decode that data…

How to find duplicates in pandas dataframe

Editing. Suppose I have the following series in pandas:>>>p 0 0.0 1 0.0 2 0.0 3 0.3 4 0.3 5 0.3 6 0.3 7 0.3 8 1.0 9 1.0 10 1.0 11 0.2 12 0.2 1…