mercredi 12 août 2015

How to apply the results of a method to a SQL query?

I am a total newbie to the coding world and I have been stuck on a problem for the last few days. Basically I am trying to add data from a JSON file to a SQLITE DB. At the moment when ever I run my code all the JSON entry's are being added to the DB file which means that there are loads of multiple entry's. What I need to do is find away of trying to check the data base for the BUYER_ID if it is contained in the JSON file do nothing and if it's not in the JSON file then add it. Below is a simplified version of my code.

Any help or even a link to website for some guidance would be appreciated.

def read_email
   Read JSON from a file, iterate over objects
    file = open("receiptdata_EXD.json")
    json = file.read
    parsed = JSON.parse(json)
    parsed['results'].each do |child|
    buyer_user_id = child['buyer_user_id']                         

    db = SQLite3::Database.open "test.db"
    row = db.get_first_row "SELECT BUYER_USER_ID FROM CUST_DATA WHERE BUYER_USER_ID='#{buyer_user_id}'" 
     end

        if buyer_user_id == row
            puts "SUCCESS"
        else 
            puts "ADD THIS TO THE DATABASE"
        end

    end 
  end
  read_email

Aucun commentaire:

Enregistrer un commentaire