Inserting data into sqlite, first goes then broken

  Kiến thức lập trình

I have a problem with my python code. I have a function which should adds something into a sqlite database. My code is following:

import sqlite3

def add(self, tablename, values: list):
    """
    Insert data into table
    """
    try:
        new_values = [f"'{value}'" if isinstance(value, str) else value for value in values]
        query = f"INSERT INTO {tablename} VALUES ({', '.join(map(str, new_values))})"
        self.cursor.execute(query) # Execute the query
        self.database_con.commit() # Commit the changes
        self.cursor.close()  # Close the cursor connection
    except Exception as e:
        return e

It expects a name of the table and a values list which should looks like that: [123, 'testmessage']. The problem now is, that the first time I add something, it works, but after that no time again.

Thanks for every help

LEAVE A COMMENT