本文整理汇总了Python中anki.db.DB.setAutocommit方法的典型用法代码示例。如果您正苦于以下问题:Python DB.setAutocommit方法的具体用法?Python DB.setAutocommit怎么用?Python DB.setAutocommit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类anki.db.DB
的用法示例。
在下文中一共展示了DB.setAutocommit方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Collection
# 需要导入模块: from anki.db import DB [as 别名]
# 或者: from anki.db.DB import setAutocommit [as 别名]
def Collection(path, lock=True, server=False, sync=True, log=False):
"Open a new or existing collection. Path must be unicode."
assert path.endswith(".anki2")
path = os.path.abspath(path)
create = not os.path.exists(path)
if create:
base = os.path.basename(path)
for c in ("/", ":", "\\"):
assert c not in base
# connect
db = DB(path)
db.setAutocommit(True)
if create:
ver = _createDB(db)
else:
ver = _upgradeSchema(db)
db.execute("pragma temp_store = memory")
if sync:
db.execute("pragma cache_size = 10000")
db.execute("pragma journal_mode = wal")
else:
db.execute("pragma synchronous = off")
db.setAutocommit(False)
# add db to col and do any remaining upgrades
col = _Collection(db, server, log)
if ver < SCHEMA_VERSION:
_upgrade(col, ver)
elif ver > SCHEMA_VERSION:
raise Exception("This file requires a newer version of Anki.")
elif create:
# add in reverse order so basic is default
addClozeModel(col)
addBasicTypingModel(col)
addForwardOptionalReverse(col)
addForwardReverse(col)
addBasicModel(col)
col.save()
if lock:
col.lock()
return col
示例2: __init__
# 需要导入模块: from anki.db import DB [as 别名]
# 或者: from anki.db.DB import setAutocommit [as 别名]
#.........这里部分代码省略.........
def setLastUsn(self, usn):
self.db.execute("update meta set lastUsn = ?", usn)
self.db.commit()
def syncInfo(self, fname):
ret = self.db.first(
"select csum, dirty from media where fname=?", fname)
return ret or (None, 0)
def markClean(self, fnames):
for fname in fnames:
self.db.execute(
"update media set dirty=0 where fname=?", fname)
def syncDelete(self, fname):
path = os.path.join(self.dir(), fname)
if os.path.exists(path):
os.unlink(path)
self.db.execute("delete from media where fname=?", fname)
def mediaCount(self):
return self.db.scalar(
"select count() from media where csum is not null")
def dirtyCount(self):
return self.db.scalar(
"select count() from media where dirty=1")
def forceResync(self):
self.db.execute("delete from media")
self.db.execute("update meta set lastUsn=0,dirMod=0")
self.db.commit()
self.db.setAutocommit(True)
self.db.execute("vacuum")
self.db.execute("analyze")
self.db.setAutocommit(False)
# Media syncing: zips
##########################################################################
def mediaChangesZip(self):
f = io.BytesIO()
z = zipfile.ZipFile(f, "w", compression=zipfile.ZIP_DEFLATED)
fnames = []
# meta is list of (fname, zipname), where zipname of None
# is a deleted file
meta = []
sz = 0
for c, (fname, csum) in enumerate(self.db.execute(
"select fname, csum from media where dirty=1"
" limit %d"%SYNC_ZIP_COUNT)):
path = os.path.join(self.dir(), fname)
fnames.append(fname)
normname = unicodedata.normalize("NFC", fname)
if csum:
self.col.log("+media zip", fname)
z.write(path, str(c))
meta.append((normname, str(c)))
sz += os.path.getsize(path)
else:
self.col.log("-media zip", fname)