SQLite Forum

DELETE FROM database WHERE rowid
Login

DELETE FROM database WHERE rowid

(1) By gastonv on 2020-08-19 18:30:27 [link] [source]

Hello, In adressen.py I have:

import sqlite3

def wisadres(id):

# Connect to database and create cursor
conn = sqlite3.connect('adressen.db')
c = conn.cursor()

c.execute("DELETE FROM adressen WHERE rowid = (?)", id)

# commit our command
conn.commit()
# close connection
conn.close()

Then, in order to run it, in adressenapp.py I have:

import adressen

adressen.wisadres('6')

When I run this, row id 6 is deleted successfully.

%Run adressenapp.py

Now I like to delete e.g. row 69 with:

import adressen

adressen.wisadres('69')

Now, with this command, I become an error message:

%Run adressenapp.py Traceback (most recent call last): File "/home/gastonv/Programmeren/Sqlite/adressenapp.py", line 19, in <module> adressen.wisadres('69') File "/home/gastonv/Programmeren/Sqlite/adressen.py", line 100, in wisadres c.execute("DELETE FROM adressen WHERE rowid = (?)", id) sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 2 supplied.

Please, what can be the reason why it doesn't works? Many thanks in advance for each answer. Kind regards, Gaston Verhulst.

(2) By David Raymond (dvdraymond) on 2020-08-19 19:05:04 in reply to 1 [link] [source]

The parameters argument in Cursor.execute is a sequence of all the parameters that will be bound. It worked in the first example because a string is a sequence of single characters, and you gave it a single character string for a query with a single parameter.

In the second example you're giving it a query with 1 parameter, but a string which is a sequence of 2 characters. So you've got 2 things to bind, but only one place for them.

Correct use would be to have the single parameter you want to bind be in a 1 element list or tuple

c.execute("DELETE FROM adressen WHERE rowid = ?;", (id,))

or

c.execute("DELETE FROM adressen WHERE rowid = ?;", [id])

(And also, id should be converted to an integer since it's a rowid, but it works ok here)

(3) By gastonv on 2020-08-19 19:44:52 in reply to 2 [source]

Hello dear David Raymond, I have to thank you very much for the quick reply.

In the mean time, I have done a test with your first suggestion with rowid = ?;", (id,) and the result was a success.

Kind regards and many thanks for the fine communication. Gaston Verhulst.