pyspider - python這個(gè)類中的方法到底有什么用處啊
問題描述
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添加``符號(hào)。比如你創(chuàng)建的table或者column里有空白字符時(shí)。
create table `hello world tb` (`column name1` INT NOT NULL AUTO_INCREMENT PRIMARY KEY)
錯(cuò)誤的查詢:select column name1 from hello world tb正確的查詢:select `column name1` from `hello world tb`
dbcur這個(gè)函數(shù)拋出未實(shí)現(xiàn)這個(gè)異常,目的是為了充當(dāng)接口,由子類去實(shí)現(xiàn)。Python里面沒有接口這個(gè)概念,所以定義接口時(shí),可以采用這種方式。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 - sublime快鍵鍵問題2. javascript - immutable配合react提升性能?3. css - 寫頁面遇到個(gè)布局問題,求大佬們幫解答,在線等,急!~4. javascript - nodejs關(guān)于進(jìn)程間發(fā)送句柄的一點(diǎn)疑問5. Apache 已經(jīng)把網(wǎng)站根目錄的改為allow from all了,但是服務(wù)器還是不能訪問?6. 實(shí)現(xiàn)bing搜索工具urlAPI提交7. 配置Apache時(shí),添加對(duì)PHP的支持時(shí)語法錯(cuò)誤8. vue.js - Vue 如何像Angular.js watch 一樣監(jiān)聽數(shù)據(jù)變化9. javascript - 移動(dòng)端上不能實(shí)現(xiàn)拖拽布局嗎?10. phpstudy8.1支持win11系統(tǒng)嗎?

網(wǎng)公網(wǎng)安備