所需积分/C币:49 2016-01-09 13:06:41 4.16MB PDF
收藏 收藏

SECOND EDITION Essential SQLAlchemy Jason Myers and rick copeland Beijing: Boston. Farnham. Sebastopol. Tokyo OREILLY Essential SQLAlchemy by Jason Myers and Rick Copeland Copyright o 2016 Jason Myers and Rick Copeland. All rights reserved Printed in the united states of america Published by o reilly Media, Inc, 1005 Gravenstein Highway North, Sebastopol, CA95472 OReilly books may be purchased for educational, business, or sales promotional use. Online editions are alsoavailableformosttitles( Editors: Dawn Schanafelt and Meghan Blanchette Indexer: Angela Howard Production Editor Shiny Kalapurakkel Interior Designer: David Futato Copyeditor: Charles Roumeliotis Cover Designer: Karen Montgomer Proofreader: Jasmine Kwityn Illustrator: Rebecca Demarest June 2008 First edition December 2015: Second edition Revision history for the second edit 2015-11-20: First release See The O reilly logo is a registered trademark of O Reilly Media, InC. Essential SQLAlchemy, the cover image, and related trade dress are trademarks of o reilly media, inc While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights 978-1-491-91646-9 ILSI Table of contents Preface Introduction to SQLAlchemy Part. SQLAlchemy〔ore 1. Schema and Types. T es Metadata Tables Columns Keys and constraints Indexes 113456779 Relationships and ForeignKey Constraints Persisting the Tables 2. Working with Data via SQLAlchemy Core................. 13 Inserting Data 13 Querying Data 17 Resultprox 18 Controlling the Columns in the Query 20 ordering 20 Limiting 21 Built-In SQL Functions and labels 22 filtering 24 Clause Elements 25 Operators 26 Boolean operators 27 Conjunctions Updating data 28 D leting data 29 31 A 32 routing 33 Chaining 34 Raw queries 35 3. Exceptions and Transactions............... 37 Exceptions 37 Attributeerror 38 Integrity error Handling Errors 41 Transactions 43 51 Testing with a Test Database 51 Using Mocks 58 5. Reflection........................ 63 Reflecting individual Tables 63 Reflecting a whole database 66 Query Building with Reflected Objects Part l. SQLAlchemy ORM 6. Defining Schema with SQLAlchemy ORM 71 Defining Tables via oRM Classes 71 Keys, Constraints, and Indexes 73 Relationships Persisting the schema 75 7. Working with Data via SQLAlchemy ORM. The Session Inserting data 80 trying Data 83 Controlling the Columns in the Query 86 Ordering 86 Limiting 87 iv Table of Contents Built-In SQL Functions and labels 88 filtering 89 Operators 90 Boolean operators 92 Conjunctions 92 Updating data 93 Deleting data 94 Joins 7 Groupin g 98 Chaining 99 Raw Queries 101 8. Understanding the Session and Exceptions The SQLAlchemy Session 105 Session states 105 Exceptions 108 Multipleresults Found Exception 108 DetachedInstanceerror 110 T ransactions 112 9. Testing with SQLAlchemy ORM 121 Testing with a Test Database 121 Using mocks 130 10. Reflection with SQLAlchemy ORM and Automap 133 Reflecting a Database with Automap 133 Reflected relationships 35 Part lll. Alembic 11. Getting Started with Alembic................... 39 Creating the Migration Environment 139 Configuring the Migration Environment 140 12. Building MigrationS.......... 143 Generating a Base Empty Migration 143 Autogenerating a Migration 145 Building a migration manually 149 3.〔 ontrolling alembic 151 Determining a Databases Migration Level 151 Table of contents Downgrading migrations 152 Marking the Database Migration Level 153 Generating SQL 154 14.〔 bokbok0k157 Hybrid Attributes 157 Association Proxy 160 Integrating SQLAlchemy with Flask 166 SQLAcodegen 169 15. Where to go from here ,.,............... 175 Index 177 Table of contents Preface We are surrounded by data everywhere, and your ability to store, update, and report on that data is critical to every application you build. Whether you are developing for the Web, the desktop or other applications, you need fast and secure access to data Relational databases are still one of the most common places to put that data SQL is a powerful language for querying and manipulating data in a database, but sometimes it's tough to integrate it with the rest of your application. You may have used string manipulation to generate queries to run over an ODBC interface, or used a db api as a python programmer. While those can be effective ways to handle data they can make security and database changes very difficult This book is about a very powerful and flexible Python library named SQlAlchemy that bridges the gap between relational databases and traditional programming While sqlalchemy allows you to drop down? into raw SQL to execute your queries, it encourages higher-level thinking through a more"Pythonic"and friendly approach to database queries and updates. It supplies the tools that let you map your applica- tions classes and objects to database tables once and then to forget about it or to return to your model again and again to fine-tune performance SQLAIchemy is powerful and flexible, but it can also be a little daunting SQLAlchemy tutorials expose only a fraction of what's available in this excellent library, and though the online documentation is extensive, it is often better as a refer ence than as a way to learn the library initially. This book is meant as a learning tool and a handy reference for when you're in implementation mode"and need an answer fast This book focuses on the 1.0 release of SQLAlchemy; however, much of what we will cover has been available for many of the previous versions. It certainly works from 0. 8 forward with minor tweaking, and most of it from 0.5 This book has been written in three major parts: SQLAlchemy Core, SQLAlchemy ORM, and an Alembic section. The first two parts are meant to mirror each other as closely as possible. We have taken care to perform the same examples in each pa art so that you can compare and contrast the two main ways of using SQLAlchemy. Th book is also written so that you can read both the SQLAlchemy Core and ORM parts or just the one suits your needs at the moment Who this book ls for This book is intended for those who want to learn more about how to use relational databases in their Python programs, or have heard about SQLAlchemy and want more information on it. To get the most out of this book, the reader should have intermediate Python skills and at least moderate exposure to SQL databases. While we have worked hard to make the material accessible, if you are just getting started with Python, we recommend reading Introducing Python by Bill Lubanovic or watch ing the Introduction to Python videos by Jessica McKellar as they are both fantastic resources. If you are new to SQL and databases, check out Learning SQL by Alan Beaulieu. These will fill in any missing gaps as you work through this book How to Use the examples Most of the examples in this book are built to be run in a read-eval-print loop (REPL). You can use the built-in Python REPl by typing python at the command prompt. The examples also work well in an ipython notebook. There are a few parts of the book, such as Chapter 4, that will direct you to create and use files instead of a REPL. The supplied example code is provided in IPython notebooks for most exam ples, and Python files for the chapters that specify to use them. You can learn more about ipython at its website Assumptions This book Makes This book assumes basic knowledge about Python syntax and semantics, particularl versions 2.7 and later. In particular, the reader should be familiar with iteration and working with objects in Python, as these are used frequently throughout the book The second part of the book deals extensively with object-oriented programming and the SQLAlchemy orm. The reader should also know basic SQL syntax and relational theory, as this book assumes familiarity with the SQL concepts of defining schema and tables along with creating SELECT, INSERT, UPDATE, and DELETE statements I Preface

试读 127P Essential.SQLAlchemy.2nd.Edition.2015.11.pdf
立即下载 低至0.43元/次 身份认证VIP会员低至7折
lgzhyan 5星推荐,很不错!
bill_law6 非常好,存着当工具书。
qqhugo 英文版的,很好用
shalei6698 很不错的一本书
yaoguoc 好货好料好楼主
ouhandsome 资源很好,谢谢分享。
crmlhwz 确实很清晰,好书
fireflyes 很不错,很清晰
lolzbq 找了很久的书,5星推荐!
tonyrush 对理解python的entity mapping很有帮助
关注 私信 TA的资源
Essential.SQLAlchemy.2nd.Edition.2015.11.pdf 49积分/C币 立即下载

试读结束, 可继续阅读

49积分/C币 立即下载 >