Python: Not able to insert data to sqlite3 database table

I’m writing a python code to insert save data to SQLite Database, but my data is not getting saved on the table of database. I have tried to search on google regarding this, but almost all the links are mentioning same thing which I have implemented. Can anyone please help me to understand, why my data is not getting saved?

Code:

import sqlite3


DATABASE_URI = 'my-passwords.db'


def create():
    connection = sqlite3.connect(DATABASE_URI)
    cursor_object = connection.cursor()

    cursor_object.execute('Drop table if exists passwords')

    table_create_query = """
    CREATE TABLE passwords(
        website varchar(2048),
        username varchar(256),
        password varchar(256)
    )
    """
    cursor_object.execute(table_create_query)

    connection.commit()
    cursor_object.close()
    connection.close()


def insert(data):
    connection = sqlite3.connect(DATABASE_URI)
    cursor_object = connection.cursor()

    cursor_object.execute('SELECT name FROM sqlite_master WHERE type=\'table\' AND name=\'abcde\';')
    if cursor_object.fetchall() == None:
        create()
    

    sqlite_insert_query = """
    INSERT INTO passwords(website,username,password)
    VALUES (?,?,?)
    """

    connection.commit()

    cursor_object.executemany(sqlite_insert_query, data)
    print(f'Total Records Inserted: {cursor_object.rowcount}')

    cursor_object.close()
    connection.close()


data = [
    ('abcd','abcd','abcd'),
    ('abcd','abcd','abcd')
]

insert(data=data)

Welcome to the community Yash!

Unfortunately, SQL is not something ActiveState provides support for.

However, posting here on the ActiveState Community is always a good idea, since some of our users may be able to help. You might also want to try posting in other coder communities like StackOverflow.

Some quick troubleshooting:

  • Test your connection first to make sure you are connecting the right database with the right credentials
  • Ensure that the table you’re querying exists, and you have permissions for it

Hope that helps,

Dana Crane