Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLite实现的开/关功能_Python_Pysqlite - Fatal编程技术网

Python SQLite实现的开/关功能

Python SQLite实现的开/关功能,python,pysqlite,Python,Pysqlite,我试图提出SQLiteDB对象,下面是它的打开/关闭代码。 这工作顺利吗?我错过了什么重要的事情吗 对于close(),我使用con.close()和cursor.close(),但我想知道cursor.close()是否是必需的 class SQLiteDB(object): def __init__(self, dbFile, connect = True): self.dbFile = dbFile self.con = None

我试图提出SQLiteDB对象,下面是它的打开/关闭代码。 这工作顺利吗?我错过了什么重要的事情吗

对于close(),我使用con.close()和cursor.close(),但我想知道cursor.close()是否是必需的

class SQLiteDB(object):
    def __init__(self, dbFile, connect = True):
        self.dbFile = dbFile

        self.con = None
        self.cursor = None

        if connect:
            self.open()

    def __del__(self):
        if self.con:
            self.close()

    def open(self):
        self.con = sqlite3.connect(self.dbFile)
        self.cursor = self.connector.cursor()
        return self.con, self.cursor

    def close(self):
        self.con.close()
        self.cursor.close()
        self.cursor = None
        self.con = None

Cursor.close()上发生的情况取决于底层数据库实现。对于SQLite,它目前可能不关闭就可以工作,但对于其他实现或未来的SQLite版本,它可能不工作,因此我建议关闭游标对象。您可以在中找到有关Cursor.close()的更多信息

此外,您的代码中似乎有输入错误:

self.connector = sqlite3.connect(self.dbFile)
应该是

self.con = sqlite3.connect(self.dbFile)
否则你的代码在我看来很好。快乐编码:)