Before I had to tinker with the next MySQL. Think about it later. Python seems to be more suitable for use with NoSQL. And then I set up the next MongoDB to play.
It was a lot better than MySQL when I tried it. Needless to say, encapsulation encapsulation.
ImportPymongoclassMONGOHP:def __init__(self,addr,port,dbname,tablename=None): Self.conn=Pymongo. Mongoclient (addr,port) self.db=Self.conn[dbname] Self.tablename=tableName Self.dbname=dbName self.addr=addr Self.port=PortifTABLENAME:SELF.TB=Self.db[tablename]#Insert a piece of data defInsert (self,data,tablename=None): MYTB=self.gettable (tableName)ifmytb:mytb.insert_one (data)returnTruereturnFalse#BULK INSERT Data defInsertall (self,list,tablename=None): MYTB=self.gettable (tableName)ifMytb:mytb.insert (list)returnTruereturnFalse#the data is inserted when the condition is met, such as when inserting a value, it is judged that there is no current data in the table. Before inserting the defInsertsig (self,reason,data,tablename): MYTB=self.gettable (tableName)ifMytb:mytb.update_one (reason,data)returnTrue#gets the specified data defFind (self,data,tablename=None): MYTB=self.gettable (tableName)ifMYTB:returnmytb.find (data)returnFalse#If you need a special action, you can get the table out of action defGetTable (self,tablename=None):ifTableName:returnSelf.db[tablename]ifSelf.tablename:returnSELF.TBreturnFalsedefRemove (self,reason=none,tablename=None): MYTB=self.gettable (tableName)ifMYTB:ifreason:mytb.remove (reason)Else: Mytb.remove ()returnTruereturnFalse#gets the specified number of table data defCount (self,tablename=none,reason=none): MYTB=self.gettable (tableName)ifMYTB:ifreason:mytb.count (reason)Else: returnMytb.count ()#Modify the default managed tables defSettable (self,tablename=None): Self.tablename=tablename
Then test it casually. Very convenient. Home Essentials.
ImportMongotestdb=mongotest. MONGOHP ("xxxx.xxxx.xxxx.xxxx", 27017,"Mongodata","Mytable1") T1={"name":"Wang"}t2={"name":"Jiang"}t3={"name":"Li"}db.insert (t1) db.insert (T2) Db.insert (T3) datas= Db.find ({"name":"Wang"}) forIteminchdatas:Print(item) CNT=Db.count ()Print(CNT) db.remove (t1) CNT=Db.count ()Print(CNT) db.insert (T1,"Mytable2") CNT=db.count ("Mytable2")Print(CNT)
Python's Pymongo