| 49 | |
| 50 | |
| 51 | def write_db(f, f1): |
| 52 | |
| 53 | f1.write("... updating database\n") |
| 54 | |
| 55 | try: |
| 56 | invoice = f['invoice'].value |
| 57 | |
| 58 | try: |
| 59 | street = f['address_street'].value |
| 60 | city = f['address_city'].value |
| 61 | zipc = f['address_zip'].value |
| 62 | country = f["address_country_code"].value |
| 63 | firstn = f['first_name'].value |
| 64 | lastn = f['last_name'].value |
| 65 | |
| 66 | except KeyError: |
| 67 | street = "" |
| 68 | city = "" |
| 69 | zipc = "" |
| 70 | country = "" |
| 71 | firstn = "" |
| 72 | lastn = "" |
| 73 | |
| 74 | try: |
| 75 | #some countries don't have states |
| 76 | state = f['address_state'].value |
| 77 | except KeyError: |
| 78 | state ="" |
| 79 | |
| 80 | if f.has_key("custom"): |
| 81 | payer_url = f["custom"].value |
| 82 | |
| 83 | query = "INSERT INTO names VALUES ('" + invoice + "', '" + \ |
| 84 | firstn + "', '" + lastn + "', '" + street + "', '" + city + "', '" + state + "', '" + zipc + "', '" + \ |
| 85 | country + "', '" + f['payer_email'].value + "', '" + \ |
| 86 | payer_url + "', '" + f['option_selection1'].value + "', '" + f['option_selection2'].value + "')" |
| 87 | |
| 88 | f1.write(query + "\n") |
| 89 | db = MySQLdb.connect(host="localhost", user="username", passwd="passwd",db="db") |
| 90 | cursor = db.cursor() |
| 91 | cursor.execute (query) |
| 92 | |
| 93 | except: |
| 94 | f1.write(''.join(format_exception(*exc_info()))) |
| 95 | |
| 96 | |
| 97 | |