def insert_into_table(curs, table, data): att_str = ad.att_string(ad.tables[table]) att_lst = ad.att_list(att_str) sql = ('INSERT INTO {} ({}) ' 'VALUES({})'.format(table, att_str, ('?, ' * len(att_lst))[:-2])) curs.execute(sql, [(data[key] if key in data.keys() else None) for key in att_lst])
class UserData(DataHandler): table = 'users' vars=ad.att_list(ad.att_string(ad.tables[table])) def __init__(self,dbh,limit=1): self._collected = open(tmp_path+'now_collected'+dbh.ts+'.tmp','w+') super(UserData,self).__init__(dbh,limit) @property def collected(self): self._collected.seek(0) return self._collected def add_to_collected(self,datum): self._collected.seek(0,2) self._collected.write(str(datum)+'\n') def extract(self,d,k): try: return d[k] except KeyError: return None def add_record(self,id,d): self.data.append([self.extract(d,v) for v in UserData.vars]) self.add_to_collected(id) super(UserData,self).add_record(d) def update(self,d): for k,v in d.iteritems(): self.add_record(k,d[k])
def insert_into_table(curs,table,data): att_str=ad.att_string(ad.tables[table]) att_lst=ad.att_list(att_str) sql=('INSERT INTO {} ({}) ' 'VALUES({})'.format(table,att_str,('?, '*len(att_lst))[:-2])) curs.execute(sql,[(data[key] if key in data.keys() else None) for key in att_lst])
def insert_into_table(curs, table, data): att_str = ad.att_string(ad.tables[table]) att_lst = ad.att_list(att_str) sql = ('INSERT INTO {} ({}) ' 'VALUES({})'.format(table, att_str, ('?, ' * len(att_lst))[:-2])) try: data_to_add = [(data[key] if key in data.keys() else None) for key in att_lst] curs.execute(sql, data_to_add) except sqlite3.IntegrityError as ie: pass
def insert_into_table(curs,table,data): att_str=ad.att_string(ad.tables[table]) att_lst=ad.att_list(att_str) sql=('INSERT INTO {} ({}) ' 'VALUES({})'.format(table,att_str,('?, '*len(att_lst))[:-2])) try: data_to_add=[(data[key] if key in data.keys() else None) for key in att_lst] curs.execute(sql,data_to_add) except sqlite3.IntegrityError as ie: pass
class FollowData(DataHandler): table = 'x_follows_y' vars=ad.att_list(ad.att_string(ad.tables[table])) def __init__(self,db_handler,limit=1): super(FollowData,self).__init__(db_handler,limit) def add_record(self,t): self.data.append(t) super(FollowData,self).add_record(t) def update(self,s): for t in s: self.add_record(t)
class UserData(DataHandler): table = 'users' vars = ad.att_list(ad.att_string(ad.tables[table])) def __init__(self, dbh): super(UserData, self).__init__(dbh) def extract(self, d, k): try: return d[k] except KeyError: return None def add_record(self, id, d): self.data.append([self.extract(d, v) for v in UserData.vars]) super(UserData, self).add_record(d) def update(self, d): for k, v in d.iteritems(): self.add_record(k, d[k])
def swap_em_in(source,target): connsourc=sqlite3.connect(source) conntarge=sqlite3.connect(target) curssourc=connsourc.cursor() curstarge=conntarge.cursor() att_str=add_data.att_string(add_data.tables['tracks']) att_lst=add_data.att_list(att_str) sql1='SELECT * FROM tracks' sql2=('INSERT INTO tracks ({}) ' 'VALUES({})'.format(att_str,('?, '*len(att_lst))[:-2])) done=0 for n,datum in enumerate(curssourc.execute(sql1)): try: curstarge.execute(sql2,datum) done += 1 except sqlite3.IntegrityError: pass if n % 500 == 0: print 'Tried: {}'.format(n) if done > 0 and done % 500 == 0: print 'Done: {}'.format(done) conntarge.commit() conntarge.commit() return True