1. Trang chủ
  2. » Tất cả

MySQL for Python (2010)

440 0 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

MySQL for Python Integrate the flexibility of Python and the power of MySQL to boost the productivity of your applications Albert Lukaszewski, PhD BIRMINGHAM - MUMBAI MySQL for Python Copyright © 2010 Packt Publishing All rights reserved No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews Every effort has been made in the preparation of this book to ensure the accuracy of the information presented However, the information contained in this book is sold without warranty, either express or implied Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals However, Packt Publishing cannot guarantee the accuracy of this information First published: September 2010 Production Reference: 1160910 Published by Packt Publishing Ltd 32 Lincoln Road Olton Birmingham, B27 6PA, UK ISBN 978-1-849510-18-9 www.packtpub.com Cover Image by Vinayak Chittar (vinayak.chittar@gmail.com) Credits Author Albert Lukaszewski Reviewers Swaroop C H Editorial Team Leader Aanchal Kumar Project Team Leader Priya Mukherji Andy Dustman Geert JM Vanderkelen Acquisition Editor Steven Wilding Development Editor Wilson D'souza Technical Editors Prashant Macha Charumati Shankaran Indexer Hemangini Bari Project Coordinator Prasad Rai Proofreader Aaron Nash Production Coordinator Shantanu Zagade Cover Work Shantanu Zagade About the Author Albert Lukaszewski is principal consultant for Lukaszewski Consulting Services in southeast Scotland He has programmed computers for 30 years Much of his experience has related to text processing, database systems, and Natural Language processing (NLP) Currently he consults on database applications for companies in the financial and publishing industries In addition to MySQL for Python, Albert Lukaszewski has also written "About Python", a column for the New York Times subsidiary, About.com Many people had a hand in this work beyond my typing at the keyboard Some contributed by their effort and others by their sacrifice Thanks to the team at Packt for their consistent understanding and support I am particularly thankful to Steven Wilding for help and support above and beyond the call of duty Thanks also to Andy Dustman, Geert Vanderkelen, and Swaroop for their helpful review of this book and for making so many significant and helpful recommendations This book would be much the poorer were it not for their suggestions To Richard Goodrich, who first introduced me to Python, thank you for liberating me from bondage to that other P-language Funny what a little problem can lead to My heartfelt thanks and appreciation go to my wife, Michelle, and my sons, Cyrus and Jacob The latter was born during the writing of this book and consistently brightens even the darkest Scottish weather with his smile I appreciate your sacrifice I could not have written this book without your support Finally, my thanks to my brother, Larry, who first introduced me to the world of computing I would probably not know anything about computer programming if you had not left me your TRS-80 So this is all your fault, and I am glad you did it About the Reviewers Swaroop C H has previously worked at Yahoo! and Adobe, has co-founded a startup, has written two technical books (one of which is used as a text book in more than ten universities worldwide), writes a popular blog that has been consistently rated one of the top ten blogs in India, and is a marathoner More details at http://www.swaroopch.com/about/ He has written two technical books—A Byte of Python and A Byte of Vim—beginner books to Python and Vim respectively They are freely available under the Creative Commons license on his website www.swaroopch.com Andy Dustman (http://profiles.google.com/farcepest) is the primary author of MySQLdb, the MySQL interface for Python The MySQL-Python project is supported and funded purely by volunteers and donations by the user community at (http://sourceforge.net/projects/mysqlpython/) Andy has been using Python since 1997, and currently works on Django applications (using MySQL, of course) when not doing system and network administration In his spare time, he rides motorcycles I would like to thank Kyle VanderBeek, who has recently become a co-developer on MySQLdb, and has helped to push me a bit to get some things done 12 years is a long time to be working on a project, and motivation is sometimes hard to come by Ed Landa, for taking a chance on a grad school dropout, and for giving me the opportunity to release MySQLdb under an open source license Laura Michaletz, who encourages me and somehow manages to make me feel like a superstar And my wife, Wendy, for being there for me for three decades Geert JM Vanderkelen is a member of the MySQL Support Team at Sun, a wholly-owned subsidiary of Oracle He is based in Germany and has worked for MySQL AB since April, 2005 Before joining MySQL he worked as developer, DBA and SysAdmin for various companies in Belgium and Germany Today Geert specializes in MySQL Cluster and works together with colleagues around the world to ensure continued support for both customers and community Geert is also the maintainer of MySQL Connector/Python Table of Contents Preface Chapter 1: Getting Up and Running with MySQL for Python Getting MySQL for Python Using a package manager (only on Linux) Using RPMs and yum Using RPMs and urpm Using apt tools on Debian-like systems Using an installer for Windows Using an egg file Using a tarball (tar.gz file) Importing MySQL for Python Accessing online help when you need it MySQLdb _mysql Connecting with a database Creating a connection object Creating a cursor object Interacting with the database Closing the connection Multiple database connections Summary Chapter 2: Simple Querying A brief introduction to CRUD Forming a query in MySQL SELECT * (asterisk) FROM staff ; (semicolon) 7 9 10 10 14 17 18 18 19 20 20 22 22 23 23 24 25 25 26 27 27 28 28 29 Table of Contents Other helpful quantifiers 29 WHERE GROUP BY HAVING ORDER BY LIMIT INTO OUTFILE 30 30 32 33 35 37 Passing a query to MySQL A simple SELECT statement Modifying the results Using user-defined variables Determining characteristics of a database and its tables Determining what tables exist Assigning each table a number Offering the options to the user Allowing the user to detail a search query Changing queries dynamically Pattern matching in MySQL queries Putting it into practice Project: A command-line search utility Preparing a database for searching Planning your work, then working your plan 37 38 39 40 41 42 43 43 44 45 45 46 48 49 50 Specifying the search term from the command-line Implementing and incorporating the other functions: -t, -f, and -o Including an option for an output file Room to grow Summary 52 55 57 57 58 Develop a well-abstracted search functionality Chapter 3: Simple Insertion Forming a MySQL insertion statement INSERT INTO Table name Column names VALUES ; (semicolon) Helpful ways to nuance an INSERT statement INSERT SELECT INSERT DELAYED… INSERT ON DUPLICATE KEY UPDATE [ ii ] 50 59 60 60 61 61 61 63 64 66 66 66 70 71 Table of Contents Passing an insertion through MySQL for Python Setting up the preliminaries A simple INSERT statement More complex INSERT commands Using user-defined variables Using metadata Querying the database for its structure Retrieving the table structure Changing insertion values dynamically Validating the value of name Validating the value of price Querying the user for a correction Passing fish and price for validation Essentials: close and commit In need of some closure What happened to commit? Why are these essentials non-essential? Project: A command-line insertion utility The necessary modules The main() thing Coding the flag system Testing the values passed by the user Try to establish a database connection Showing the tables Showing the table structure, if desired Accepting user input for the INSERT statement Building the INSERT statement from the user input and executing it Committing changes and closing the connection Coding the other functions valid_digit() and valid_string() valid_table() query() 72 72 73 75 75 77 78 80 82 83 83 84 84 85 85 85 85 86 86 87 88 88 89 90 90 91 92 93 93 93 94 94 Calling main() Room to grow Summary 95 99 100 Chapter 4: Exception Handling Why errors and warnings are good for you Errors versus warnings: There's a big difference The two main errors in MySQLdb DatabaseError InterfaceError Warnings in MySQL for Python [ iii ] 101 101 104 104 105 105 105 ... of MySQLdb, the MySQL interface for Python The MySQL- Python project is supported and funded purely by volunteers and donations by the user community at (http://sourceforge.net/projects/mysqlpython/)... support for both customers and community Geert is also the maintainer of MySQL Connector /Python Table of Contents Preface Chapter 1: Getting Up and Running with MySQL for Python Getting MySQL for Python. . .MySQL for Python Integrate the flexibility of Python and the power of MySQL to boost the productivity of your applications Albert Lukaszewski, PhD BIRMINGHAM - MUMBAI MySQL for Python

Ngày đăng: 13/04/2019, 01:41

Xem thêm:

Mục lục

    Chapter 1: Getting Up and Running with MySQL for Python

    Getting MySQL for Python

    Using a package manager (only on Linux)

    Using RPMs and yum

    Using RPMs and urpm

    Using apt tools on Debian-like systems

    Using an installer for Windows

    Using an egg file

    Importing MySQL for Python

    Accessing online help when you need it

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN