Changeset 754


Ignore:
Timestamp:
Jul 1, 2013, 4:37:55 PM (6 years ago)
Author:
Nick Burch
Message:

Update execute statements - must pass a tuple or map, not a single value, and fetch single value responses by index not name

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/npemap.org.uk/scripts/generic-python-import/generic_importer.py

    r753 r754  
    103103                source_id = None
    104104
    105                 sql = "SELECT id FROM sources WHERE name = %s"
     105                sql = "SELECT id FROM sources WHERE name = %(name)s"
    106106                sth = dbh.cursor()
    107                 sth.execute(sql, self.source_name)
     107                sth.execute(sql, {'name':self.source_name})
    108108                ids = sth.fetchall()
    109109                sth.close()
    110110                if len(ids) == 1:
    111111                        for id in (ids):
    112                                 source_id = id.id
     112                                source_id = id[0]
    113113                else:
    114114                        print "Unable to find ID for source '%s' - error code %d" % (self.source_name, len(ids))
     
    120120                reason_id = None
    121121                while reason_id == None:
    122                         sql = "SELECT id FROM delete_reasons WHERE reason = %s"
     122                        sql = "SELECT id FROM delete_reasons WHERE reason = %(reason)s"
    123123                        sth = dbh.cursor()
    124                         sth.execute(sql, self.delete_reason)
     124                        sth.execute(sql, {'reason':self.delete_reason})
    125125                        ids = sth.fetchall()
    126126                        sth.close()
    127127                        if len(ids) == 1:
    128128                                for id in (ids):
    129                                         reason_id = id.id
     129                                        reason_id = id[0]
    130130                        else:
    131131                                sth = dbh.cursor()
    132                                 sth.execute("INSERT INTO delete_reasons (reason) VALUES (%s)", self.delete_reason)
     132                                sth.execute("INSERT INTO delete_reasons (reason) VALUES (%(reason)s)", {'reason':self.delete_reason})
    133133                                sth.close()
    134134
     
    195195                # Make sure that un-deleted ones come first, so that if one postcode
    196196                #  has deleted and undeleted forms, we prefer the undeleted form
    197                 sql = "SELECT id, outward, inward, deleted, delete_reason FROM postcodes WHERE source = %s ORDER BY deleted"
     197                sql = "SELECT id, outward, inward, deleted, delete_reason FROM postcodes WHERE source = %(source)s ORDER BY deleted"
    198198                sth = dbh.cursor()
    199                 sth.execute(sql, source_id)
     199                sth.execute(sql, {'source':source_id})
    200200
    201201                spcs = {}
     
    217217
    218218                # And for interest, grab the raw deleted count
    219                 sql = "SELECT COUNT(id) FROM postcodes WHERE source = %s AND deleted"
     219                sql = "SELECT COUNT(id) FROM postcodes WHERE source = %(source)s AND deleted"
    220220                sth = dbh.cursor()
    221                 sth.execute(sql, source_id)
     221                sth.execute(sql, {'source':source_id})
    222222                raw_deleted_count = sth.fetchone()[0]
    223223                sth.close()
Note: See TracChangeset for help on using the changeset viewer.