Book Image

Python 3 Web Development Beginner's Guide

By : Michel Anders
Book Image

Python 3 Web Development Beginner's Guide

By: Michel Anders

Overview of this book

<p>Building your own Python web applications provides you with the opportunity to have great functionality, with no restrictions. However, creating web applications with Python is not straightforward. Coupled with learning a new skill of developing web applications, you would normally have to learn how to work with a framework as well.</p> <p><em>Python 3 Web Development Beginner's Guide</em> shows you how to independently build your own web application that is easy to use, performs smoothly, and is themed to your taste – all without having to learn another web framework.</p> <p>Web development can take time and is often fiddly to get right. This book will show you how to design and implement a complex program from start to finish. Each chapter looks at a different type of web application, meaning that you will learn about a wide variety of features and how to add them to your custom web application. You will also learn to implement jQuery into your web application to give it extra functionality. By using the right combination of a wide range of tools, you can have a fully functional, complex web application up and running in no time.</p>
Table of Contents (19 chapters)
Python 3 Web Development Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – retrieving information with select statements


In SQL, the select statement can be used to retrieve records from a database. How would you express a query to retrieve all tasks belonging to the user john?

Answer: select * from task where user_id = 'john'

We may implement this in Python as follows (only relevant lines shown, complete implementation is available as taskdb2.py):

Chapter4/tasktdb2.py

connection.row_factory = sqlite3.Row

sql = """select * from task where user_id = 'john'"""
cursor.execute(sql)
tasks = cursor.fetchall()
for t in tasks:
  print(t['duedate'],t['description'])

What just happened?

The first line in the code is normally placed just after establishing a connection to the database and ensures that any row returned from a fetchone() or fetchall() method are not plain tuples, but sqlite3.Row objects. These objects behave just like tuples, but their fields can be indexed by the name of the column they represent as well.

The query is executed by passing it...