Skip to content

Instantly share code, notes, and snippets.

@aq1
Created September 6, 2016 16:58
Show Gist options
  • Save aq1/cccecabd481e46289077beb0cfedbe52 to your computer and use it in GitHub Desktop.
Save aq1/cccecabd481e46289077beb0cfedbe52 to your computer and use it in GitHub Desktop.
import os
import sqlite3
# try:
# os.remove('db.sqlite3')
# except FileNotFoundError:
# pass
conn = sqlite3.connect('db.sqlite3')
cursor = conn.cursor()
cursor.execute('create table if not exists installs (created datetime, mobile_app integer, country text)')
cursor.execute('create table if not exists purchases (created datetime, mobile_app integer, country text, installed datetime, revenue float)')
def insert_data():
import csv
def get_csv(filename):
with open(filename, "rb") as csvfile:
data = csv.reader(csvfile)
# skip header
data.next()
for row in data:
yield row
for table, query in zip(('installs', 'purchases'), ('insert into installs values (?, ?, ?)', 'insert into purchases values (?, ?, ?, ?, ?)')):
rows = []
for i, x in enumerate(get_csv('%s.csv' % table), 1):
rows.append(x)
if i % 1000 == 0:
cursor.executemany(query, rows)
rows = []
conn.commit()
print(cursor.execute('select count(rowid) from installs where created between "2016-05-02 00:00:00" and "2016-05-09 23:59:59"').fetchall())
print(cursor.execute('select count(rowid) from purchases where installed between "2016-05-02 00:00:00" and "2016-05-09 23:59:59"').fetchall())
# 2016-05-01 00:00:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment