久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

python連接mongodb數(shù)據(jù)庫操作數(shù)據(jù)示例

瀏覽:3日期:2022-07-03 15:34:52

作者: wyh草樣

出處:https://www.cnblogs.com/wyh0923/p/14047466.html

1、數(shù)據(jù)庫配置類 MongoDBConn.py

#encoding=utf-8’’’Mongo Conn連接類’’’import pymongoclass DBConn: conn = None servers = 'mongodb://localhost:27017' def connect(self): self.conn = pymongo.Connection(self.servers) def close(self): return self.conn.disconnect() def getConn(self): return self.conn

2、ngoDemo.py 類

#encoding=utf-8’’’Mongo操作DemoDone:’’’import MongoDBConndbconn = MongoDBConn.DBConn()conn = Nonelifeba_users = Nonedef process(): #建立連接 dbconn.connect() global conn conn = dbconn.getConn() #列出server_info信息 print conn.server_info() #列出全部數(shù)據(jù)庫 databases = conn.database_names() print databases #刪除庫和表 dropTable() #添加數(shù)據(jù)庫lifeba及表(collections)users createTable() #插入數(shù)據(jù) insertDatas() #更新數(shù)據(jù) updateData() #查詢數(shù)據(jù) queryData() #刪除數(shù)據(jù) deleteData() #釋放連接 dbconn.close()def insertDatas(): datas=[{'name':'steven1','realname':'測試1','age':25}, {'name':'steven2','realname':'測試2','age':26}, {'name':'steven1','realname':'測試3','age':23}] lifeba_users.insert(datas)def updateData(): ’’’只修改最后一條匹配到的數(shù)據(jù) 第3個(gè)參數(shù)設(shè)置為True,沒找到該數(shù)據(jù)就添加一條 第4個(gè)參數(shù)設(shè)置為True,有多條記錄就不更新 ’’’ lifeba_users.update({’name’:’steven1’},{’$set’:{’realname’:’測試1修改’}}, False,False)def deleteData(): lifeba_users.remove({’name’:’steven1’})def queryData(): #查詢?nèi)繑?shù)據(jù) rows = lifeba_users.find() printResult(rows) #查詢一個(gè)數(shù)據(jù) print lifeba_users.find_one() #帶條件查詢 printResult(lifeba_users.find({’name’:’steven2’})) printResult(lifeba_users.find({’name’:{’$gt’:25}}))def createTable(): ’’’創(chuàng)建庫和表’’’ global lifeba_users lifeba_users = conn.lifeba.usersdef dropTable(): ’’’刪除表’’’ global conn conn.drop_database('lifeba')def printResult(rows): for row in rows: for key in row.keys():#遍歷字典 print row[key], #加, 不換行打印 print ’’if __name__ == ’__main__’: process()

以上就是python連接mongodb數(shù)據(jù)庫操作數(shù)據(jù)示例的詳細(xì)內(nèi)容,更多關(guān)于python連接mongodb的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 日韩专区在线播放 | 欧美成人一区二免费视频软件 | 毛片免费看 | 成人在线观看网 | 国产一区二区精品 | 国产区视频 | 国产精品一区二区在线观看 | 国产精品精品视频一区二区三区 | 99热精品在线| 欧美xxxx片 | 日韩精品一区二区三区在线 | 国产女人爽到高潮免费视频 | 日本不卡免费新一二三区 | a免费在线 | 亚洲免费婷婷 | 久久一二区 | 日韩免费高清视频 | 中文字幕一区在线观看 | 精品久久av | 一区二区三区在线看 | 日韩av免费 | 久久爱成人 | 一级毛片免费 | 91九色porny首页最多播放 | 天天综合网91 | 在线观看亚洲大片短视频 | 精品一区在线 | 视频一区免费观看 | 久久综合久久久 | 日韩欧美中字 | 亚洲精品久久久一区二区三区 | 久久这里只有精品首页 | 国产精品美女www爽爽爽软件 | 免费成人高清 | 国产精品高清在线 | 在线免费毛片 | 国产欧美精品一区二区色综合 | 在线区 | 九九在线精品 | 精品久久久久久久久久久 | 亚洲天堂久 |