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. android - weex 項目createInstanceReferenceError: Vue is not defined2. PHPExcel表格導入數據庫怎么導入3. android - 哪位大神知道java后臺的api接口的對象傳到前端后輸入日期報錯,是什么情況?求大神指點4. javascript - 如圖,百度首頁,查看源代碼為什么什么都沒有?5. pdo 寫入到數據庫的內容為中文的時候寫入亂碼6. vue2.0+webpack 如何使用bootstrap?7. PHP類封裝的插入數據,總是插入不成功,返回false;8. docker綁定了nginx端口 外部訪問不到9. mac連接阿里云docker集群,已經卡了2天了,求問?10. ddos - apache日志很多其它網址,什么情況?
