本文整理匯總了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))
示例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))
示例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
示例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
示例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")
示例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))
示例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))
示例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))
示例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))
示例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))
示例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))
示例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))
示例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))
示例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")
示例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")