模型 - 视图 - 控制器模式实现中的问题

时间:2018-06-04 01:48:22

标签: python design-patterns

我正在运行一个用于学习python中的模型 - 视图 - 控制器模式的示例,但是代码给出了错误。我试图调试代码,但我无法找到主要根/原因。删除close连接系统有效,但代码有什么问题?你能告诉我什么是错的吗?

# Filename: mvc.py 
import sqlite3 
import types 


class DefectModel: 
    def getDefectList(self, component): 
        query = '''select ID from defects where Component = '%s' ''' %component 
        defectlist = self._dbselect(query) 
        list = [] 
        for row in defectlist: 
            list.append(row[0])
            return list 

    def getSummary(self, id): 
        query = '''select summary from defects where ID = '%d' ''' % id 
        summary = self._dbselect(query) 
        for row in summary: 
            return row[0] 

    def _dbselect(self, query): 
        connection = sqlite3.connect('example.db') 
        cursorObj = connection.cursor() 
        results = cursorObj.execute(query) 
        connection.commit()
        cursorObj.close() 
        return results 

class DefectView: 
    def summary(self, summary, defectid): 
        print("#### Defect Summary for defect# %d ####\n %s"  % (defectid,summary) )

    def defectList(self, list, category): 
        print("#### Defect List for %s ####\n" % category )
        for defect in list: 
            print(defect )

class Controller: 
    def __init__(self): pass 

    def getDefectSummary(self, defectid): 
        model = DefectModel() 
        view = DefectView() 
        summary_data = model.getSummary(defectid) 
        return view.summary(summary_data, defectid) 

    def getDefectList(self, component): 
        model = DefectModel() 
        view = DefectView() 
        defectlist_data = model.getDefectList(component) 
        return view.defectList(defectlist_data, component)

这与run.py。

有关
#run.py
import mvc 
controller = mvc.Controller()
 # Displaying Summary for defect id # 2
print(controller.getDefectSummary(2)) 
# Displaying defect list for 'ABC' Component print 
controller.getDefectList('ABC')

如果您需要创建数据库,可在此处获取:

# Filename: datbase.py 
import sqlite3 
import types 
# Create a database in RAM
db = sqlite3.connect('example.db')

# Get a cursor object
cursor = db.cursor()
    cursor.execute("drop table defects")

    cursor.execute("CREATE TABLE defects(id INTEGER PRIMARY KEY, Component TEXT, Summary TEXT)")

   cursor.execute("INSERT INTO defects VALUES (1,'XYZ','File doesn‘t get deleted')")
    cursor.execute("INSERT INTO defects VALUES (2,'XYZ','Registry doesn‘t get created')")
    cursor.execute("INSERT INTO defects VALUES (3,'ABC','Wrong title gets displayed')")

    # Save (commit) the changes
    db.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
db.close()

我的错误如下:

> Windows PowerShell Copyright (C) Microsoft Corporation. All rights
> reserved.
> 
> PS E:\Projects\test> & python e:/Projects/test/mvc.py
> Traceback (most recent call last):   File
> "e:/Projects/test/mvc.py", line 56, in <module>
>     import mvc   File "e:\Projects\test\mvc.py", line 65, in <module>
>     cursor.execute("drop table defects") sqlite3.OperationalError: no such table: defects PS E:\Projects\test> & python
> e:/Projects/ramin/mvc.py Traceback (most recent call last):  
> File "e:/Projects/test/mvc.py", line 56, in <module>
>     import mvc   File "e:\Projects\test\mvc.py", line 80, in <module>
>     print(controller.getDefectSummary(2))   File "e:\Projects\test\mvc.py", line 44, in getDefectSummary
>     summary_data = model.getSummary(defectid)   File "e:\Projects\test\mvc.py", line 18, in getSummary
>     for row in summary: sqlite3.ProgrammingError: Cannot operate on a closed cursor. PS E:\Projects\test>

1 个答案:

答案 0 :(得分:-1)

我怀疑问题出在这一行:cursor.execute("drop table defects")

也许您在之前的运行中删除了该表,并且由于它不再存在,sqlite3会引发OperationalError异常。

在您的代码中有一条注释表明您使用的是内存中的sqlite数据库,但事实并非如此。这是您创建内存数据库的方法:

db = sqlite3.connect(:memory:)

如果您使用内存数据库,则不需要删除任何内容,因为您在运行脚本时即时创建数据库。

注意:去年我想更好地理解MVC,所以我写了一系列关于它的文章。 Here是我使用SQLite作为模型的存储后端的那个。