Mysql
#Encoding:utf-8ImportMysqldbconn= MySQLdb.connect (host='***.***.***.***', port=3306, user='***', passwd='***', db='****',) cur=conn.cursor () AA= Cur.execute ('SELECT * from User_info')PrintAA#How much data is printed in the tableinfo =cur.fetchmany (AA) forIiinchInfo:Printiicur.close () conn.commit () Conn.close ( )Mongdb
#Encoding:utf-8 fromPymongoImportmongoclientImportJSONImportsysreload (SYS) sys.setdefaultencoding ('Utf-8')#client = mongoclient (' ***.***.***.*** ', 27017)#Admin Database has account, connection-authentication-switch Library#uri = ' mongodb://' + user + ': ' + pwd + ' @ ' + server + ': ' + port + '/' + db_nameURI ='Mongodb://ntalker:[email Protected]***.***.***.***:27017/db_name'Client=mongoclient (URI) db_name=' Db_name'DB=Client[db_name]Printdb.collection_names () collection= db['Test_message']#access a row of data for a table?PrintCollection.find_one ()#get all the columns forRowsinchCollection.find_one ():Printrows#Accessing table data, specifying columnsitem =Collection.find () forRowsinchItem: _id= rows["_id"] Data= Json.dumps (rows['msg'], ensure_ascii=False)PrintData Json_to_python=json.loads (data) text= [] forContentinchJson_to_python:src_type= content['Source_type'] txt= content["content"] ifSrc_type = = U'Visitor' and notSTR (TXT). StartsWith (U"https://") and notSTR (TXT). StartsWith (U"/ http"): Text.append (content["content"]) Printtext rows["text"] =text Collection.save (rows)
Python link Database