Script:
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()
Consistent to MVC Pattern,
the model runs a business logic that pings a database for an id and gets the corresponding name,
the controller accepts the request, sends it to the model, receives the return data from model and communicates to the view,
and the view represents the return data.
You could find a similar example on stackoverflow.
1 comment:
Great example!
Thanks,
Andrew.
Post a Comment