Skip to content

Instantly share code, notes, and snippets.

@shotahorii
Last active May 29, 2017 23:58
Show Gist options
  • Save shotahorii/274b832570205e439bd54a3a8ce0bde6 to your computer and use it in GitHub Desktop.
Save shotahorii/274b832570205e439bd54a3a8ce0bde6 to your computer and use it in GitHub Desktop.
A python module storing some common functions about sqlite
import sqlite3
import pandas as pd
import functools as ft
class SqliteHandler:
def __init__(self, dbname):
self.dbname = dbname
def df2table(self, tablename, df):
# connect to db
conn = sqlite3.connect(self.dbname)
cur = conn.cursor()
# define columns
cols = list(df.columns)
cols_str = ft.reduce(lambda x,y: x+','+y, cols)
qmarks_str = ft.reduce(lambda x,y: x+','+y, ['?']*len(cols))
# create table if not exists
cur.execute("CREATE TABLE IF NOT EXISTS "+tablename+" ("+cols_str+");")
# format data to insert to db
to_db = []
for index, row in df.iterrows():
y = tuple(map(lambda x: row[x], cols))
to_db.append(y)
# insert data
cur.executemany("INSERT INTO "+tablename+" ("+cols_str+") VALUES ("+qmarks_str+");", to_db)
conn.commit()
# close db connection
conn.close()
def table2df(self, query):
# make a connection
conn = sqlite3.connect(self.dbname)
cur = conn.cursor()
# execute query
cur.execute(query)
l = cur.fetchall()
# close the connection
cur.close()
# convert to data frame
return pd.DataFrame(l) # no column name tho
def columns(self, tablename):
# make a connection
conn = sqlite3.connect(self.dbname)
cur = conn.cursor()
# get column names
cur.execute( "PRAGMA table_info("+tablename+")")
h = cur.fetchall()
return list(map(lambda x: x[1], h)) # formatting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment