python 2.7 - Put information from a dabtabse file into lists -
import sqlite3 db = sqlite3.connect('newdb.db') team_list = ['munster', 'leinster', 'ulster', 'glasgow'] cursor = db.cursor() in range(len(team_list)): team_names = team_list[i].upper() searchstr = '%' + team_names + '%' cursor.execute('select * tickets name ?', (searchstr,)) teams_points = cursor.fetchall() print teams_points cursor.close() db.close()
this python code used display data in table 'tickets' in newdb.db. have list team names , want able search these team names in database , calculate information on tickets sold. picture of database
[(u'munster', 5, u'first round'), (u'munster', 5, u'first round'), (u'munster', 8, u'second round'), (u'munster', 10, u'both rounds')] [(u'leinster', 2, u'second round'), (u'leinster', 16, u'first round'), (u'leinster', 5, u'both rounds'), (u'leinster', 6, u'both rounds'), (u'leinster', 3, u'first round')] [(u'ulster', 10, u'second round')] [(u'glasgow', 4, u'first round')]
above output when run script, i want able put each team list as
team_list=['team_name', 'total first round tickets', 'second round tickets'] munster_list = ['munster', '20', '18'] leinster_list = ['leinster','30','13'] ulster_list = ['ulster','0','10'] glasgow_list = ['glasgow','4','0']
so print list can use print munster_list
use group 1 output row rows in each group. use case expressions sum values:
select name, sum(case when type in ('first round', 'both rounds') amount else 0 end) "first round tickets", sum(case when type in ('second round', 'both rounds') amount else 0 end) "second round tickets" tickets group name order name;
Comments
Post a Comment