Write the database insertion logic