#
# addorg.py: add organization data using dynamic queries
#
# PostgreSQL 9 version
#
from db import conn, curs, Error, pmark

#
# Look at the code in cratdb.py, or your database, for column names
#
# We need to insert the organization name and address - the
# database should automagically provide the primary key for each row,
# and other fields will be NULL until other software changes them
#
# If you haven't provided an automatically-generated primary
# key then you may have to modify the data to include it.
#
TBL = "sponsorship_organization"
COLS = "name addr1 addr2 addr3 addr4 addr5".split()
COLS = ["org"+col for col in COLS]
#
# Feel free to add more data if you want to
#
data = [
    ('Holden Web LLC', '9725 Hampton Road', 'Fairfax Station', 'VA', '22039', 'USA'),
    ('Python Software Foundation', 'PO Box 653', 'Ipswich', 'MA', '01938', 'USA'),
    ('Holden Web Ltd', '22 Braehead Place', 'Linlithgow', 'West Lothian', 'EH49 6EF', 'UK')
    ]

# 1. Generate a comma-separated list of fieldnames
flist = ", ".join(COLS)
# 2. Generate a comma-separated list of parameter markers
plist = ", ".join([pmark] * len(COLS))
# 3. Execute an appropriate SQL INSERT statement
sql = "INSERT INTO %s (%s) VALUES (%s)" % (TBL, flist, plist)
for row in data:
    curs.execute(sql, row)

conn.commit()

