diff --git a/main.py b/main.py index b6b9ef6..94aab59 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,23 @@ import cgi import datetime +import configparser import pymysql +def getDatabase(path = "config.cfg"): + config = configparser.RawConfigParser() + config.read(path) + + host = config.get("Database", "host") + user = config.get("Database", "user") + pwd = config.get("Database", "pass") + name = config.get("Database", "name") + + conn = pymysql.connect(host, user, pwd, name) + cur = conn.cursor() + + return conn, cur + def application(env, re): if env["REQUEST_METHOD"] == "POST": args = cgi.parse_qs(env['wsgi.input'].readline().decode(), True) @@ -41,5 +56,9 @@ def application(env, re): timestr = timestamp.strftime('%Y-%m-%d %H:%M:%S') - sql = "INSERT INTO tracker(ts, device, lat, lon, alt) VALUES (\"%s\", \"%s\", %s, %s, %s);" % (timestr, device, str(latitude), str(longitude), str(altitude) or "None") + conn, cur = getDatabase() + sql = "INSERT INTO tracker(ts, device, lat, lon, alt) VALUES (%s, %s, %s, %s, %s);" + + cur.execute(sql, (timestr, device, str(latitude), str(longitude), str(altitude) if altitude != None else None)) + conn.commit()