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)