Last active
December 27, 2015 09:49
-
-
Save lovevn/7306704 to your computer and use it in GitHub Desktop.
Python MVC Example
This file contains 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
import sqlite3 | |
class MVCModel: | |
def request(self, id): | |
# Would query database... | |
conn = sqlite3.connect('querydb') | |
c = conn.cursor() | |
results = c.execute('''select name from data where id = %d''' %id) | |
conn.commit() | |
c.close() | |
for row in results: | |
name = row[0] | |
return { "id" : id, "name": name} | |
class MVCController: | |
def __init__(self): | |
self.model = MVCModel() | |
self.view = MVCView() | |
def main(self): | |
post = self.model.request(1) | |
self.view.show(post) | |
class MVCView: | |
def show(self, post): | |
print "%(id)s %(name)s" % post | |
Controller = MVCController() | |
Controller.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment