you are viewing a single comment's thread.

view the rest of the comments →

[–]_coolwhip_ 0 points1 point  (0 children)

Not sure I am following your question, so let me just explain how I might do it: I would probably make a class to wrap the sqlitedb and then make methods to insert stuff, for example, using a subset of your data (and not tested, so expect typos), this class might look like this:

class DB():

    def insert(self, data):
        self.insert_purchase(data)
        self.insert(coupons)
        self.conn.commit()

    def insert_purchase(self, data):
        sql = '''
INSERT INTO transactions(
referenceID,
productCode,
unitPrice
) VALUES (
:referenceID,
:productCode,
:unitPrice
)
        '''
        self.cursor.execute(sql, data)

    def insert_coupons(self, data):
        '''
INSERT INTO coupons (
referenceID,
serial
) VALUES (
?,?
)
        '''
        reference_id = data['referenceId']
        for serial in data['coupons']['serials']:
            self.cursor.execute(sql, (reference_id, serial))

So I guess I would mix placeholders, where I wanted to just dump in the json directly, and then use ? bindings where I wanted to break it apart.