ems/src/modules/database.py

44 lines
1.1 KiB
Python
Raw Normal View History

'''
Created on 26.11.2013
@author: rauchp
'''
from MySQLdb import connect
def setup(conf):
connection = connect(host = conf['mySQL_server'],
user = conf['mySQL_user'],
passwd = conf['mySQL_pass'],
db = conf['mySQL_database'],
port = int(conf['mySQL_port']))
cursor = connection.cursor()
return cursor
def loop(cursor, item):
sql_values = 'SELECT * FROM %s ORDER BY timestamp DESC LIMIT 1'
sql_collums = 'SHOW COLUMNS FROM %s'
path = item.split('/')
if path[0] == '':
path.remove('')
collums = []
cursor.execute(sql_collums % path[len(path) -1])
for cul in cursor:
collums.append(cul[0])
cursor.execute(sql_values % path[len(path) -1])
for row in cursor:
values = row
result = {}
for i in range(len(collums)):
result[collums[i]] = values[i]
# for p in range(len(path)):
# result = { path.pop() : result }
result = {item : result}
return result