from config import conftools
import datetime
import MySQLdb
import traceback

conf = conftools.get_config()["DEFAULT"]

def connectdb():
	conn = MySQLdb.connect(host=conf["db.host"],
					  user=conf["db.username"],
					  passwd=conf["db.password"],
					  db=conf["db.database"])
	return conn

def dbinsert(table,columns,values,params):
	conn = connectdb()
	x = conn.cursor()
	
	query = "INSERT INTO " + table + " ("
	first_col = True
	for col in columns:
		query += ("," if not(first_col) else "")
		first_col = False
		query += col
	query += ") VALUES ("
	first_val = True
	for val in values:
		query += ("," if not(first_val) else "")
		first_val = False
		query += val
	query += ");"
		
	try:
		x.execute(query,params)
		conn.commit()
	except:
		conn.rollback()
	
	id = x.lastrowid
	conn.close()
	return id	
	
def save_response(response):
	rJson = response.json()
	
	for result in rJson["results"]:
		result_id = dbinsert("result",("search_time","currency","tot_price"),("NOW()","%s","%s"),(rJson["currency"],result["fare"]["total_price"]))




