curs = conn.cursor()
query = """
CREATE TABLE IF NOT EXISTS """+TABLENAME+""" (
state CHAR(2) NOT NULL,
city VARCHAR NOT NULL,
population BIGINT
CONSTRAINT my_pk PRIMARY KEY (state, city))
"""
curs.execute(query)
Bulk insert a set of data, using nested arrays for each record, and executing multiple upserts.
Finally, run a query to return an aggregated group-by and return as a Dictionary object.
curs = conn.cursor(cursor_factory=phoenixdb.cursor.DictCursor)
query = """SELECT state as "State",count(city) as "City Count",sum(population) as "Population Sum"
FROM us_population
GROUP BY state
ORDER BY sum(population) DESC"""
curs.execute(query)
print(curs.fetchall())
When the above is run in a session, it will return the following results.