Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Writing json to sqlite3 but with a condition to remove a person from sqlite3 if person is not in json file

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
    {"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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading