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函數(shù)是干什么的,看起來像是返回一段字符串dbcur怎么用來調(diào)用的呢,上面說dbcur應(yīng)該重寫,在子類中重寫嗎,然后怎么調(diào)用啊
pyspider代碼https://github.com/binux/pysp...
問題解答
回答1:escape 是給string添加``符號。比如你創(chuàng)建的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這個函數(shù)拋出未實(shí)現(xiàn)這個異常,目的是為了充當(dāng)接口,由子類去實(shí)現(xiàn)。Python里面沒有接口這個概念,所以定義接口時,可以采用這種方式。DbBase只付責(zé)構(gòu)建sql語句,具體使用何種數(shù)據(jù)庫由子類實(shí)現(xiàn),好處是可以適配不同的數(shù)據(jù)庫。
源碼:
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()
相關(guān)文章:
1. javascript - vue2如何獲取v-model變量名2. javascript - 求幫助 , ATOM不顯示界面!!!!3. html5 - HTML代碼中的文字亂碼是怎么回事?4. javascript - vue2.0中,$refs對象為什么用駝峰的方式獲取不到屬性?5. python bottle跑起來以后,定時執(zhí)行的任務(wù)為什么每次都重復(fù)(多)執(zhí)行一次?6. 解決Android webview設(shè)置cookie和cookie丟失的問題7. javascript - nodejs使用mongoose連接數(shù)據(jù)庫,使用post提交表單在后臺,后臺處理后調(diào)用res.redirect()跳轉(zhuǎn)界面無效?8. javascript - 能否讓vue-cli的express修改express重啟服務(wù)9. python - 爬蟲模擬登錄后,爬取csdn后臺文章列表遇到的問題10. html5 - 急求?被公司問住了
