search

Home  >  Q&A  >  body text

pyspider - python这个类中的方法到底有什么用处啊

class BaseDB:

    '''
    BaseDB
    dbcur should be overwirte
    '''
    __tablename__ = None
    placeholder = '%s'
    maxlimit = -1

    @staticmethod
    def escape(string):
        return '`%s`' % string

    @property
    def dbcur(self):
        raise NotImplementedError

escape函数是干什么的,看起来像是返回一段字符串
dbcur怎么用来调用的呢,上面说dbcur应该重写,在子类中重写吗,然后怎么调用啊

pyspider代码
https://github.com/binux/pysp...

怪我咯怪我咯2785 days ago626

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:28:38

    escape is to add the `` symbol to string. For example, when there are blank characters in the table or column you created.

    create table `hello world tb` (`column name1` INT NOT NULL AUTO_INCREMENT PRIMARY KEY)
    

    Bad query: select column name1 from hello world tb
    正确的查询:select `column name1` from `hello world tb`

    dbcurThis function throws the unimplemented exception. The purpose is to act as an interface and be implemented by subclasses. There is no concept of interface in Python, so this method can be used when defining an interface. DbBase is only responsible for constructing sql statements. The specific database to be used is implemented by subclasses. The advantage is that it can adapt to different databases.

    Source code:

    if __name__ == "__main__":
        import sqlite3
    
        class DB(BaseDB):
            __tablename__ = "test"
            placeholder = "?"
    
            def __init__(self):
                self.conn = sqlite3.connect(":memory:")
                cursor = self.conn.cursor()
                cursor.execute(
                    '''CREATE TABLE `%s` (id INTEGER PRIMARY KEY AUTOINCREMENT, name, age)'''
                    % self.__tablename__
                )
    
            @property
            def dbcur(self):
                return self.conn.cursor()
    

    reply
    0
  • Cancelreply