-2

OperationalError
sqlite3.OperationalError: no such column: ceica

This is the error message that I receive when I try to delete a row from my databse Note: I already have implemented functions for adding, searching in tables and worked perfectly fine

HTML button:

 {% for row in rows %}
        <p>
            <b>Nume: </b>{{row[1]}} {{row[2]}}, <b>Varsta: </b>{{row[3]}} <br>
            <b>Telefon: </b>{{row[4]}}, <b>Localitate:</b>{{row[5]}}, <b>E-mail: </b>{{row[6]}}
            <button type="button" class="close"><a href="/stergere/pacient/{{row[1]}}">X</a></button>
        </p>
        <hr>
    {% endfor %}

Python function:

@auth.route('/stergere/<tabel>/<element>')
def stergereInTabel(tabel,element):
    connection=sqlite3.connect('database.db')
    cursor=connection.cursor()
    if tabel=='pacient':
        #print("DELETE FROM ? WHERE nume=?",(tabel,str(element),))
        #cursor.execute("DELETE FROM ? WHERE nume=?",(tabel,str(element),))
        print("DELETE FROM {} WHERE nume={x}".format(tabel,x=element))
        cursor.execute("DELETE FROM {} WHERE nume={x}".format(tabel,x=element))
    return render_template("{}.html".format(tabel),user=current_user)

I tried every combination possible with converting element to string, using .format for both parameters, using tuple method for both but none worked.

Barmar
  • 741,623
  • 53
  • 500
  • 612
sergiutzuu
  • 11
  • 3
  • 1
    You're making us guess where the error is. Please edit the question and add the whole error traceback message. – John Gordon Dec 08 '22 at 22:41

1 Answers1

0

The problem is that you didn't put quotes around {x}, so the ceica is being treated as a column name, not a string literal.

You can use string formatting to substitute the table name, but you should use a cursor.execute() parameter for values.

cursor.execute(f"DELETE FROM {tabel} WHERE nume = ?", (element,))
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I tried every way, if I do it as you say, it redirects me to the right page but it doesn t delete the row in my database – sergiutzuu Dec 08 '22 at 23:02
  • If it's not deleting anything, that means there are no rows that match the condition. Make sure you don't have any extra spaces in `element`. – Barmar Dec 08 '22 at 23:04