Skip to content

Instantly share code, notes, and snippets.

@stripe-q
Created January 30, 2018 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stripe-q/f0f587340642dce5e9951f2d215cc845 to your computer and use it in GitHub Desktop.
Save stripe-q/f0f587340642dce5e9951f2d215cc845 to your computer and use it in GitHub Desktop.
import sqlite3 as sql
from contextlib import contextmanager
@contextmanager
def open_db(filename):
try:
db = sql.connect(filnname)
c = db.cursor
c.row_factory = sql.Row
yield c
finally:
db.commit()
db.close()
DBFILENAME = 'mydata.db'
def parse_member_info(filename):
with open(filename) as f:
with open_db(DBFILENAME) as c:
for line in f:
_id, _name, *addr, num, employee_number = line.split()
addr = ' '.join(addr)
query = 'INSERT INTO members (id, nmae, address, num, employee_number)'\
'VALUES (?, ?, ?, ?, ?)'
c.execute(query, (_id, _name, addr, num, employee_number))
def parse_addres_info(filename):
with open(filename) as f:
with open_db(DBFILENAME) as c:
for line in f:
region, city, area, is_dev = line.split()
is_dev = is_dev == 'o'
query = 'INSERT INTO city_info (region, city, area, isdev) VALUES' \
'(?, ?, ?, ?)'
c.execute(query, (region, city, area, is_dev))
def parse_weather_info(filename):
with open(filename) as f:
with open_db(DBFILENAME) as c:
for line in f:
temp, hum, annoying = line.split()
query = 'INSERT INTO weather_info (temp, hum, ann) VALUES (?, ?, ?)'
c.execute(query, (temp, hum, annoying))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment