资源描述:
《python data analysis working with databases》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、WorkingwithDatabasesIfyouworkwithdata,soonerorlater,youwillcomeintocontactwithdatabases.Thischapterintroducesvariousdatabases(relationalandNoSQL)andrelatedAPIs.Arelationaldatabaseisadatabasethathasacollectionoftablescontainingdataorganizedbytherelationsbetweendataitems.Arelationshipcanbesetu
2、pbetweeneachrowinthetableandarowinanothertable.Arelationaldatabasedoesnotjustpertaintorelationshipsbetweentables;firstly,ithastodowiththerelationshipbetweencolumnsinsideatable(obviously,columnswithinatablehavetoberelated,forinstance,anamecolumnandanaddresscolumninacustomertable);secondly,itr
3、elatestoconnectionsbetweentables.NoSQL(NotOnlySQL)databasesareundergoingsubstantialgrowthinBigDataandwebapplications.NoSQLsystemsmayinfactpermitSQL-likequerylanguagestobeemployed.ThemainthemeofNoSQLdatabasesisallowingdatatobestoredinamoreflexiblemannerthantherelationalmodelpermits.Thismaymea
4、nnothavingadatabaseschemaoraflexibledatabaseschema.Ofcourse,theflexibilityandspeedmaycomeatapricesuchaslimitedsupportforconsistenttransactions.NoSQLdatabasescanstoredatausingadictionarystyle,inacolumn-orientedway,asdocuments,objects,graphs,tuples,oracombinationthereof.Thetopicsofthischaptera
5、relistedasfollows:•Lightweightaccesswithsqlite3•Accessingdatabasesfrompandas•InstallingandsettingupSQLAlchemy•PopulatingadatabasewithSQLAlchemy•QueryingthedatabasewithSQLAlchemy•PonyORM•Dataset—databasesforlazypeople•PyMongoandMongoDB•StoringdatainRedis•ApacheCassandraWorkingwithDatabasesLig
6、htweightaccesswithsqlite3SQLiteisaverypopularrelationaldatabase.It'sverylightweightandusedbymanyapplications,forinstance,webbrowserssuchasMozillaFirefox.Thesqlite3moduleinthestandardPythondistributioncanbeusedtoworkwithaSQLitedatabase.Withsqlite3,wecaneitherstorethedatabaseinafileorkeepitinR
7、AM.Forthisexample,wewilldothelatter.Importsqlite3asfollows:importsqlite3Aconnectiontothedatabaseisneededtoproceed.Ifwewantedtostorethedatabaseinafile,wewouldprovideafilename.Instead,dothefollowing:withsqlite3.connect(":memory:")ascon:Thewithstateme