简体   繁体   中英

How to turn an interger list to single value tuples

I wrote a code to delete some users from the database, but it said Incorrect number of bindings supplied. The current statement uses 1, and there are 4 supplied Incorrect number of bindings supplied. The current statement uses 1, and there are 4 supplied .

def delete_user(self, user_id):
    stmt = "DELETE FROM user WHERE user_id = (?)"
    args = user_id
    try:
        self.conn.executemany(stmt, (args,))
        self.conn.commit()
    except sqlite3.Error as e:
        logging.error(str(e))

My part main code:

    deleted_user = []
    deleted_user.append(1384995383)
    deleted_user.append(1667596031)
    deleted_user.append(1332658866)
    deleted_user.append(1295962235)

    print(deleted_user)
    db.delete_user(deleted_user)

I found this answer: sqlite3: Incorrect number of bindings supplied. The current statement uses 1, and there are 5 supplied

I try to use tuple(user_id) to convert it to a tuple, but the result was not I'm except

Origin deleted_user list:

[1384995383, 1667596031, 1332658866, 1295962235]

After tuple(user_id) result:

(1384995383, 1667596031, 1332658866, 1295962235)

What I Expectation:

[(1384995383,), (1667596031,), (1332658866,), (1295962235,)]

I tried searching, but I couldn't get the answers I wanted:(

Try this:

deleted_user.append((1384995383,))
def delete_user_ids(self, user_ids):
    stmt = "DELETE FROM user WHERE user_id = (?)"
    try:
        self.conn.executemany(stmt, user_ids)
#                                   ^^^^^^^^
user_ids = []
user_ids.append((1384995383,))
user_ids.append((1667596031,))
user_ids.append((1332658866,))
user_ids.append((1295962235,))
delete_user_ids(user_ids)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM