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


Python Db.newQuery方法代码示例

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


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

示例1: BookDbLayer

# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import newQuery [as 别名]
class BookDbLayer(object):
    def __init__(self, connectionName=None, db_to_clone=None):
        super(BookDbLayer, self).__init__()
        self._db = Db(connectionName, db_to_clone)

    def __getattr__(self, name):
        return getattr(self._db, name)

    def create(self, databaseName, name, description, genrelist, grouplist):
        db = self._db
        db.open(databaseName)
        db.transaction()
        try:
            for statement in schema.split(';'):
                db.execUpdate(statement)

            for pair in [('version', 1),
                         ('name', name),
                         ('description', description)]:
                db.execUpdate("INSERT INTO info(name, value) VALUES(?,?)",
                              pair[0], pair[1])

            for genre in genrelist:
                db.execUpdate("INSERT INTO libgenrelist(genreid, genrecode, genredesc) VALUES(?,?,?)",
                              genre[0], genre[1], genre[2])

            for groupname in grouplist:
                db.execUpdate("INSERT INTO libgrouplist(groupname) VALUES(?)",
                              groupname)

        except DatabaseError:
            db.rollback()
            raise
        else:
            if not db.commit():
                raise DatabaseError, db.lastError().text()

    def getDbProperty(self, name):
        return self._db.execScalar("SELECT value FROM info WHERE name = ?",
                                   name)

    def setDbProperty(self, name, value):
        self._db.execUpdate("INSERT OR REPLACE INTO info(name, value) VALUES(?,?)",
                            name, value)

    def getBookCount(self):
        return self._db.execScalar("SELECT COUNT(*) FROM libbook").toLongLong()[0]

    def getFilenameByBookId(self, bookid):
        return self._db.execScalar("SELECT filename FROM libfilename WHERE bookid = ?",
                                   bookid).toString()

    def getGroupsBookIn(self, bookid):
        query = self._db.newQuery()
        query.prepare("SELECT groupid, groupname FROM libgroup JOIN libgrouplist USING(groupid) WHERE bookid = ?")
        query.addBindValue(bookid)
        if not query.exec_():
            raise DatabaseError, query.lastError().text()
        groups = []
        while query.next():
            groups.append((query.value(0).toInt()[0],
                           query.value(1).toString()))
        return groups

    def getGroupsBookNotIn(self, bookid):
        query = self._db.newQuery()
        query.prepare("SELECT groupid, groupname FROM libgrouplist WHERE groupid NOT IN (SELECT groupid FROM libgroup WHERE bookid = ?)")
        query.addBindValue(bookid)
        if not query.exec_():
            raise DatabaseError, query.lastError().text()
        groups = []
        while query.next():
            groups.append((query.value(0).toInt()[0],
                           query.value(1).toString()))
        return groups

    def addBookToGroup(self, bookid, groupid):
        self._db.execUpdate("INSERT INTO libgroup(bookid, groupid) VALUES(?,?)",
                            bookid, groupid)

    def removeBookFromGroup(self, bookid, groupid):
        self._db.execUpdate("DELETE FROM libgroup WHERE bookid = ? AND groupid = ?",
                            bookid, groupid)

    def addGroup(self, groupname):
        self._db.execUpdate("INSERT OR IGNORE INTO libgrouplist(groupname) VALUES(?)",
                            groupname)

    def removeGroup(self, groupid):
        db = self._db
        db.transaction()
        try:
            db.execUpdate("DELETE FROM libgroup WHERE groupid = ?",
                          groupid)
            db.execUpdate("DELETE FROM libgrouplist WHERE groupid = ?",
                          groupid)
        except DatabaseError:
            db.rollback()
            raise
        else:
#.........这里部分代码省略.........
开发者ID:Apkawa,项目名称:pyhomelib,代码行数:103,代码来源:bookdblayer.py


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