Created
April 23, 2012 12:11
-
-
Save bbrodriges/2470549 to your computer and use it in GitHub Desktop.
sqlalchemy strange
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: UTF-8 -*- | |
"""This module contains all methods to manipulate with database.""" | |
#THIRD_PARTY MODULES | |
from sqlalchemy import * | |
#STANDART MODULES | |
import time | |
from hashlib import md5 | |
class Database: | |
""" Initialize this class to create an instance of database. | |
NOTE: don't forget to monkeypatch SA to use with python 3 | |
http://www.sqlalchemy.org/trac/browser/README.py3k | |
""" | |
def __init__(self): | |
""" Creates connection to database """ | |
self.engine = create_engine('mysql://root@localhost/bigzypy', | |
encoding="utf-8") | |
#self.engine.echo = True | |
meta = MetaData(bind=self.engine, reflect=True) | |
self.users_table = meta.tables['users'] | |
self.videos_table = meta.tables['videos'] | |
def update_user(self, login, new_values): | |
""" @type login str | |
@type new_values dict | |
Updates user's fields with given values in database. | |
new_values must be a dict with db fields as keys and | |
new values as correspondent values. | |
""" | |
# TODO: universal update method | |
query = 'UPDATE users SET :field = :value WHERE login = :login' | |
field, value = new_values.popitem() | |
data = {'field': field, 'value': value, 'login': login} | |
result = self.engine.execute(text(query), data) | |
print(result) | |
if result: | |
return True | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment