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...
问题解答
回答1:escape 是给string添加``符号。比如你创建的table或者column里有空白字符时。
create table `hello world tb` (`column name1` INT NOT NULL AUTO_INCREMENT PRIMARY KEY)
错误的查询:select column name1 from hello world tb正确的查询:select `column name1` from `hello world tb`
dbcur这个函数抛出未实现这个异常,目的是为了充当接口,由子类去实现。Python里面没有接口这个概念,所以定义接口时,可以采用这种方式。DbBase只付责构建sql语句,具体使用何种数据库由子类实现,好处是可以适配不同的数据库。
源码:
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__ )@propertydef dbcur(self): return self.conn.cursor()
相关文章:
1. javascript - 一排三个框,各个框的间距是15px,距离外面的白框间距也是15px,这个css怎么写?2. javascript - jQuery post()方法,里面的请求串可以转换为GBK编码么?可以的话怎样转换?3. html5 - vue-cli 装好了 新建项目的好了,找不到项目是怎么回事?4. django - python 2层文件夹导入5. python - 用urllib抓取网页上的下载链接,目标文件是xls形式,但发现抓下来的xls是空表,里面只有一句报错信息,求帮助。6. 用CSS3 box-sizing 属性实现两个并排的容器,如果想让容器中间有间隔该如何实现7. mysql - C#连接数据库时一直这一句出问题int i = cmd.ExecuteNonQuery();8. css - ul ol前边的标记如何调整样式呢9. javascript - vue 手机端项目在进入主页后 在进入子页面,直接按返回出现空白情况10. python3.x - python 中的maketrans在utf-8文件中该怎么使用

网公网安备