I have a json file (that’s dynamic and is generated daily) which is like this for yesterday:
[
{"name": "saeed1", "age": 29},
{"name": "saeed2", "age": 30},
{"name": "saeed3", "age": 31}
]
And this is my python code to include them in a sqlite file:
with open('my_file.json', 'r') as file:
names = json.loads(file.read())
filename = "names.sqlite3"
table_name = 'names'
sqlite3_connection = sqlite3.connect(filename)
sqlite3_connection.execute(
f'''
CREATE TABLE IF NOT EXISTS {table_name}
(name TEXT,
date TEXT,
sent_status TEXT,
PRIMARY KEY(name)
);
'''
)
for name in names:
sqlite3_connection.execute(
f"""
INSERT OR IGNORE INTO {table_name} (name, date, sent_status)
VALUES (
'{name['name']}',
'{(datetime.datetime.now()).strftime('%Y-%m-%d')}',
'not_yet'
)
"""
)
sqlite3_connection.commit()
sqlite3_connection.close()
Now suppose this is my json file for today:
[
{"name": "saeed2", "age": 30},
{"name": "saeed3", "age": 31},
{"name": "saeed4", "age": 32}
]
The difference between the first and the second json file is that saeed1 is removed in the second json output and saeed4 is new.
With the code I have, new names are inserted in the database and because of INSERT OR IGNORE INTO, it doesn’t write duplicate exact names.
The question based on the above is: How can I remove saeed1 when I run my python code for today’s json file?
>Solution :
Make a list of all the names that are in the JSON file. Then delete all the rows where the names are not in this list.
all_names = list({i['name'] for i in names})
placeholders = ','.join(['?'] * len(all_names))
sql = f'DELETE FROM {table_name} WHERE name NOT IN ({placeholders})'
sqlite3_connection.execute(sql, all_names)