当前位置: 首页>>代码示例>>Python>>正文


Python sqlite3.DatabaseError方法代码示例

本文整理汇总了Python中sqlite3.DatabaseError方法的典型用法代码示例。如果您正苦于以下问题:Python sqlite3.DatabaseError方法的具体用法?Python sqlite3.DatabaseError怎么用?Python sqlite3.DatabaseError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sqlite3的用法示例。


在下文中一共展示了sqlite3.DatabaseError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __setitem__

# 需要导入模块: import sqlite3 [as 别名]
# 或者: from sqlite3 import DatabaseError [as 别名]
def __setitem__(self, key, value):
        uri, metadata = value
        try:
            with sqlite3.connect(self.dbfile) as db:
                cursor = db.cursor()
                cursor.execute("PRAGMA foreign_keys=ON")
                dbid = cursor.execute("SELECT id FROM pyro_names WHERE name=?", (key,)).fetchone()
                if dbid:
                    dbid = dbid[0]
                    cursor.execute("DELETE FROM pyro_metadata WHERE object=?", (dbid,))
                    cursor.execute("DELETE FROM pyro_names WHERE id=?", (dbid,))
                cursor.execute("INSERT INTO pyro_names(name, uri) VALUES(?,?)", (key, uri))
                if metadata:
                    object_id = cursor.lastrowid
                    for m in metadata:
                        cursor.execute("INSERT INTO pyro_metadata(object, metadata) VALUES (?,?)", (object_id, m))
                cursor.close()
                db.commit()
        except sqlite3.DatabaseError as e:
            raise NamingError("sqlite error in setitem: " + str(e)) 
开发者ID:irmen,项目名称:Pyro5,代码行数:22,代码来源:nameserver.py

示例2: optimized_metadata_search

# 需要导入模块: import sqlite3 [as 别名]
# 或者: from sqlite3 import DatabaseError [as 别名]
def optimized_metadata_search(self, metadata_all=None, metadata_any=None, return_metadata=False):
        try:
            with sqlite3.connect(self.dbfile) as db:
                if metadata_any:
                    # any of the given metadata
                    params = list(metadata_any)
                    sql = "SELECT id, name, uri FROM pyro_names WHERE id IN (SELECT object FROM pyro_metadata WHERE metadata IN ({seq}))" \
                          .format(seq=",".join(['?'] * len(metadata_any)))
                else:
                    # all of the given metadata
                    params = list(metadata_all)
                    params.append(len(metadata_all))
                    sql = "SELECT id, name, uri FROM pyro_names WHERE id IN (SELECT object FROM pyro_metadata WHERE metadata IN ({seq}) " \
                          "GROUP BY object HAVING COUNT(metadata)=?)".format(seq=",".join(['?'] * len(metadata_all)))
                result = db.execute(sql, params).fetchall()
                if return_metadata:
                    names = {}
                    for dbid, name, uri in result:
                        metadata = {m[0] for m in db.execute("SELECT metadata FROM pyro_metadata WHERE object=?", (dbid,)).fetchall()}
                        names[name] = uri, metadata
                else:
                    names = {name: uri for (dbid, name, uri) in result}
                return names
        except sqlite3.DatabaseError as e:
            raise NamingError("sqlite error in optimized_metadata_search: " + str(e)) 
开发者ID:irmen,项目名称:Pyro5,代码行数:27,代码来源:nameserver.py

示例3: catch_corrupt_db

# 需要导入模块: import sqlite3 [as 别名]
# 或者: from sqlite3 import DatabaseError [as 别名]
def catch_corrupt_db(f, self, *a, **kw):
    """A decorator which wraps HistoryAccessor method calls to catch errors from
    a corrupt SQLite database, move the old database out of the way, and create
    a new one.
    """
    try:
        return f(self, *a, **kw)
    except DatabaseError:
        if os.path.isfile(self.hist_file):
            # Try to move the file out of the way
            base,ext = os.path.splitext(self.hist_file)
            newpath = base + '-corrupt' + ext
            os.rename(self.hist_file, newpath)
            self.init_db()
            print("ERROR! History file wasn't a valid SQLite database.",
            "It was moved to %s" % newpath, "and a new file created.")
            return []
        
        else:
            # The hist_file is probably :memory: or something else.
            raise 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:23,代码来源:history.py

示例4: retrieve

# 需要导入模块: import sqlite3 [as 别名]
# 或者: from sqlite3 import DatabaseError [as 别名]
def retrieve(self, key, unserialize=False):
        retVal = None
        if key and (self._write_cache or os.path.isfile(self.filepath)):
            hash_ = HashDB.hashKey(key)
            retVal = self._write_cache.get(hash_)
            if not retVal:
                while True:
                    try:
                        for row in self.cursor.execute("SELECT value FROM storage WHERE id=?", (hash_,)):
                            retVal = row[0]
                    except sqlite3.OperationalError, ex:
                        if not "locked" in getSafeExString(ex):
                            raise
                    except sqlite3.DatabaseError, ex:
                        errMsg = "error occurred while accessing session file '%s' ('%s'). " % (self.filepath, ex)
                        errMsg += "If the problem persists please rerun with `--flush-session`"
                        raise SqlmapDataException, errMsg
                    else:
                        break 
开发者ID:krintoxi,项目名称:NoobSec-Toolkit,代码行数:21,代码来源:hashdb.py

示例5: test

# 需要导入模块: import sqlite3 [as 别名]
# 或者: from sqlite3 import DatabaseError [as 别名]
def test(self):
        bin_path = os.path.join("bin", "test_package")
        self.run(bin_path, run_environment=True)
        # test that the database is encrypted when sqlcipher is used
        con = sqlite3.connect("test.db")
        cursor = con.cursor()
        try:
            cursor.execute("select * from tab_sample")
        except sqlite3.DatabaseError:
            assert self.options["sqlpp11-connector-sqlite3"].with_sqlcipher
            self.output.info("database is encrypted with sqlcipher")
            return
        assert not self.options["sqlpp11-connector-sqlite3"].with_sqlcipher
        self.output.info("database is not encrypted") 
开发者ID:conan-io,项目名称:conan-center-index,代码行数:16,代码来源:conanfile.py

示例6: __getitem__

# 需要导入模块: import sqlite3 [as 别名]
# 或者: from sqlite3 import DatabaseError [as 别名]
def __getitem__(self, item):
        try:
            with sqlite3.connect(self.dbfile) as db:
                result = db.execute("SELECT id, uri FROM pyro_names WHERE name=?", (item,)).fetchone()
                if result:
                    dbid, uri = result
                    metadata = {m[0] for m in db.execute("SELECT metadata FROM pyro_metadata WHERE object=?", (dbid,)).fetchall()}
                    return uri, metadata
                else:
                    raise KeyError(item)
        except sqlite3.DatabaseError as e:
            raise NamingError("sqlite error in getitem: " + str(e)) 
开发者ID:irmen,项目名称:Pyro5,代码行数:14,代码来源:nameserver.py

示例7: __len__

# 需要导入模块: import sqlite3 [as 别名]
# 或者: from sqlite3 import DatabaseError [as 别名]
def __len__(self):
        try:
            with sqlite3.connect(self.dbfile) as db:
                return db.execute("SELECT count(*) FROM pyro_names").fetchone()[0]
        except sqlite3.DatabaseError as e:
            raise NamingError("sqlite error in len: " + str(e)) 
开发者ID:irmen,项目名称:Pyro5,代码行数:8,代码来源:nameserver.py

示例8: __contains__

# 需要导入模块: import sqlite3 [as 别名]
# 或者: from sqlite3 import DatabaseError [as 别名]
def __contains__(self, item):
        try:
            with sqlite3.connect(self.dbfile) as db:
                return db.execute("SELECT EXISTS(SELECT 1 FROM pyro_names WHERE name=? LIMIT 1)", (item,)).fetchone()[0]
        except sqlite3.DatabaseError as e:
            raise NamingError("sqlite error in contains: " + str(e)) 
开发者ID:irmen,项目名称:Pyro5,代码行数:8,代码来源:nameserver.py

示例9: __delitem__

# 需要导入模块: import sqlite3 [as 别名]
# 或者: from sqlite3 import DatabaseError [as 别名]
def __delitem__(self, key):
        try:
            with sqlite3.connect(self.dbfile) as db:
                db.execute("PRAGMA foreign_keys=ON")
                dbid = db.execute("SELECT id FROM pyro_names WHERE name=?", (key,)).fetchone()
                if dbid:
                    dbid = dbid[0]
                    db.execute("DELETE FROM pyro_metadata WHERE object=?", (dbid,))
                    db.execute("DELETE FROM pyro_names WHERE id=?", (dbid,))
                db.commit()
        except sqlite3.DatabaseError as e:
            raise NamingError("sqlite error in delitem: " + str(e)) 
开发者ID:irmen,项目名称:Pyro5,代码行数:14,代码来源:nameserver.py

示例10: clear

# 需要导入模块: import sqlite3 [as 别名]
# 或者: from sqlite3 import DatabaseError [as 别名]
def clear(self):
        try:
            with sqlite3.connect(self.dbfile) as db:
                db.execute("PRAGMA foreign_keys=ON")
                db.execute("DELETE FROM pyro_metadata")
                db.execute("DELETE FROM pyro_names")
                db.commit()
            with sqlite3.connect(self.dbfile, isolation_level=None) as db:
                db.execute("VACUUM")  # this cannot run inside a transaction.
        except sqlite3.DatabaseError as e:
            raise NamingError("sqlite error in clear: " + str(e)) 
开发者ID:irmen,项目名称:Pyro5,代码行数:13,代码来源:nameserver.py

示例11: optimized_prefix_list

# 需要导入模块: import sqlite3 [as 别名]
# 或者: from sqlite3 import DatabaseError [as 别名]
def optimized_prefix_list(self, prefix, return_metadata=False):
        try:
            with sqlite3.connect(self.dbfile) as db:
                names = {}
                if return_metadata:
                    for dbid, name, uri in db.execute("SELECT id, name, uri FROM pyro_names WHERE name LIKE ?", (prefix + '%',)).fetchall():
                        metadata = {m[0] for m in db.execute("SELECT metadata FROM pyro_metadata WHERE object=?", (dbid,)).fetchall()}
                        names[name] = uri, metadata
                else:
                    for name, uri in db.execute("SELECT name, uri FROM pyro_names WHERE name LIKE ?", (prefix + '%',)).fetchall():
                        names[name] = uri
                return names
        except sqlite3.DatabaseError as e:
            raise NamingError("sqlite error in optimized_prefix_list: " + str(e)) 
开发者ID:irmen,项目名称:Pyro5,代码行数:16,代码来源:nameserver.py

示例12: remove_items

# 需要导入模块: import sqlite3 [as 别名]
# 或者: from sqlite3 import DatabaseError [as 别名]
def remove_items(self, items):
        try:
            with sqlite3.connect(self.dbfile) as db:
                db.execute("PRAGMA foreign_keys=ON")
                for item in items:
                    dbid = db.execute("SELECT id FROM pyro_names WHERE name=?", (item,)).fetchone()
                    if dbid:
                        dbid = dbid[0]
                        db.execute("DELETE FROM pyro_metadata WHERE object=?", (dbid,))
                        db.execute("DELETE FROM pyro_names WHERE id=?", (dbid,))
                db.commit()
        except sqlite3.DatabaseError as e:
            raise NamingError("sqlite error in remove_items: " + str(e)) 
开发者ID:irmen,项目名称:Pyro5,代码行数:15,代码来源:nameserver.py

示例13: everything

# 需要导入模块: import sqlite3 [as 别名]
# 或者: from sqlite3 import DatabaseError [as 别名]
def everything(self, return_metadata=False):
        try:
            with sqlite3.connect(self.dbfile) as db:
                names = {}
                if return_metadata:
                    for dbid, name, uri in db.execute("SELECT id, name, uri FROM pyro_names").fetchall():
                        metadata = {m[0] for m in db.execute("SELECT metadata FROM pyro_metadata WHERE object=?", (dbid,)).fetchall()}
                        names[name] = uri, metadata
                else:
                    for name, uri in db.execute("SELECT name, uri FROM pyro_names").fetchall():
                        names[name] = uri
                return names
        except sqlite3.DatabaseError as e:
            raise NamingError("sqlite error in everything: " + str(e)) 
开发者ID:irmen,项目名称:Pyro5,代码行数:16,代码来源:nameserver.py

示例14: CheckDatabaseError

# 需要导入模块: import sqlite3 [as 别名]
# 或者: from sqlite3 import DatabaseError [as 别名]
def CheckDatabaseError(self):
        self.assertTrue(issubclass(sqlite.DatabaseError, sqlite.Error),
                        "DatabaseError is not a subclass of Error") 
开发者ID:vmware-archive,项目名称:vsphere-storage-for-docker,代码行数:5,代码来源:dbapi.py

示例15: CheckDataError

# 需要导入模块: import sqlite3 [as 别名]
# 或者: from sqlite3 import DatabaseError [as 别名]
def CheckDataError(self):
        self.assertTrue(issubclass(sqlite.DataError, sqlite.DatabaseError),
                        "DataError is not a subclass of DatabaseError") 
开发者ID:vmware-archive,项目名称:vsphere-storage-for-docker,代码行数:5,代码来源:dbapi.py


注:本文中的sqlite3.DatabaseError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。