SQL – Python and Databases (Continued)

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
Advertisements

2010/11 : [1]Building Web Applications using MySQL and PHP (W1)MySQL Recap.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Introduction to Python
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
MS Access Database Connection. Database? A database is a program that stores data and records in a structured and queryable format. The tools that are.
Data Abstraction UW CSE 140 Winter What is a program? – A sequence of instructions to achieve some particular purpose What is a library? – A collection.
Input, Output, and Processing
An Introduction to Structured Query Language (SQL) John H. Porter University of Virginia Department of Environmental Sciences.
CSC 2720 Building Web Applications Database and SQL.
IT:Network:Applications.  “Business runs on databases…” ◦ Understatement!  Requirements  Installation  Creating Databases  SIMPLE query ◦ Just enough.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
Applications Development
Variables, Expressions, and Statements
Files Tutor: You will need ….
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Python Let’s get started!.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Jennifer Widom Relational Databases The Relational Model.
Radoslav Georgiev Telerik Corporation
Introduction to pysqlite
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Creating Database Objects
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
SQL – Python and Databases
Topics Designing a Program Input, Processing, and Output
Adapters and Converters
Aggregate Functions and Collations
Python Let’s get started!.
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Completing the Problem-Solving Process
2.5 Another Java Application: Adding Integers
SQL – Data types.
Case Statements and Functions
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Database Construction (and Usage)
SQL – Column constraints
MS Access Database Connection
Thinking about programming
SQL – Parameterized Queries
Data Abstraction UW CSE 160 Winter 2016.
Relational Databases The Relational Model.
Relational Databases The Relational Model.
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
PHP.
Creating Tables & Inserting Values Using SQL
CREATE, INSERT, SELECT.
SQL-1 Week 8-9.
Recap Week 2 and 3.
Variables, Data Types & Math
(more) Python.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Thinking about programming
Handling Data in PL/SQL Blocks
Data Type Conversion ICS2O.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Data Types and Maths Programming Guides.
Creating Database Objects
JDBC II IS
SQL (Structured Query Language)
Python fundamental.
Getting Started in Python
Presentation transcript:

SQL – Python and Databases (Continued)

Tokens You can parse a query into tokens (words/units) and afterwards decide what they mean. Take CSE450 (Translation of Programming Languages, aka Compilers) if you want to learn more. " CREATE TABLE myTable(name TEXT,age INTEGER) ; " ["CREATE", "TABLE", "myTable", "(", "name", "TEXT", ",", "age", "INTEGER", ")", ";"]

TEXT Be sure not to break up TEXT values into different tokens (this changes the whitespace in them) "INSERT INTO myTable VALUES ('My name');" WRONG: ["INSERT", "INTO", "myTable", "VALUES", "(", "'", "My", "name", "'", ")", ";"] RIGHT: ["INSERT", "INTO", "myTable", "VALUES", "(", "'My name'", ")", ";"]

Now what? Once you have a list of tokens, you can process them. All statements end with a semicolon Check that the last token is a semicolon (raise an error if it isn't) Remove it from the list of tokens Some tokens should be combined to a single value

Integer Integers can have a negative sign in front of them, so if you have the tokens: [..., ",", "-", "23782", ","...] You should convert it to: [..., ",", "-23782", ",", ...]

REAL REAL values can have a negative sign, optional 0 or other integer, a decimal point, then another integer For project 2, all REAL values will have a decimal point and no exponential portion So, ["-", "53", ".", "89273"] Becomes, ["-53.89273"]

Conversion The queries are always just strings, but to store data in a database, you need to convert the token to a python object. "'Howdy!'" becomes "Howdy!" (the single quotes are not part of the string) "-4" becomes -4 (you can use the int function) "-34.5352" becomes -34.5352 (you can use the float function) "NULL" becomes None

Where should you store your database? Possible locations: In the connection object Problem: what if you connect to the same database multiple times In a file Problem: you don't know how to write and read multiple relations to a file We'll learn how later In a module-level (global) variable This is what I recommend while we are only making transient databases

Possible Scaffold for project 2 _ALL_DATABASES = {} def connect(filename): return Connection(filename) class Connection(): def __init__(filename): #connect or create database def execute(query): #your_code_here that works with a Database instance class Database(): ... class Table(): ... class Row(): ...

Why is _ALL_DATABASES a dictionary? Because it can expand as rows are added Because it can hold a mapping of attributes to values Because it can hold a mapping of filenames to databases Because it can hold a mapping of table names to table instances