Python and SQLite. COMP Lecture 25 March 26th, 2012 Mathieu Perreault. Monday, 26 March, 12

Similar documents
CS108 Lecture 19: The Python DBAPI

Python and Databases

POSTGRESQL - PYTHON INTERFACE

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus Lecture #23: SQLite

FIT 100 More Microsoft Access and Relational Databases Creating Views with SQL

Bases de Dades: introduction to SQL (indexes and transactions)

Introduction to pysqlite

CF SQLite Code Generator User Manual V

CS 2316 Homework 9a GT Pizza Login Due: Wednesday November 6th Out of 100 points. Premise

II (The Sequel) We will use the following database as an example throughout this lab, found in students.db.

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

CS 2316 Homework 9a GT Room Reservation Login

15-388/688 - Practical Data Science: Relational Data. J. Zico Kolter Carnegie Mellon University Spring 2018

INFO 1103 Homework Project 2

Introduction to Databases and SQL

A Sample Approach to your Project

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

SQL 2 (The SQL Sequel)

Traffic violations revisited

CSE 344 JANUARY 19 TH SUBQUERIES 2 AND RELATIONAL ALGEBRA

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Database Systems CSE 414

Welcome to CS61A! Last modified: Thu Jan 23 03:58: CS61A: Lecture #1 1

Kaivos User Guide Getting a database account 2

Databases in Python. MySQL, SQLite. Accessing persistent storage (Relational databases) from Python code

Alyssa Grieco. Data Wrangling Final Project Report Fall 2016 Dangerous Dogs and Off-leash Areas in Austin Housing Market Zip Codes.

NCSS: Databases and SQL

Announcements. Subqueries. Lecture Goals. 1. Subqueries in SELECT. Database Systems CSE 414. HW1 is due today 11pm. WQ1 is due tomorrow 11pm

INTRODUCTION TO DATA SCIENCE

Wri$ng Django Extensions for PostgreSQL. Jonathan S. Katz Exco Ventures Postgres Open Sep 15, 2011

SQL I: Introduction. Relational Databases. Attribute. Tuple. Relation

PyMySQL Documentation

CS 2316 Exam 4 Fall 2011

SQL STORED ROUTINES. CS121: Relational Databases Fall 2017 Lecture 9

CS 2316 Homework 9a Login Due: Friday, November 2nd, before 11:55 PM Out of 100 points. Premise

HIVE MOCK TEST HIVE MOCK TEST III

Lesson 13 Transcript: User-Defined Functions

How to Remove Duplicate Rows in Excel

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

CS 1110, LAB 1: PYTHON EXPRESSIONS.

Whole Group: Basics of Graphing on your TI Calculator

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Getting Started with SQL

Part 2,Number Systems Questions

Overview of Query Evaluation

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

Rows and Range, Preceding and Following

WEEK 3 TERADATA EXERCISES GUIDE

Lecture #12: Quick: Exceptions and SQL

Databases. Course October 23, 2018 Carsten Witt

printf( Please enter another number: ); scanf( %d, &num2);

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

Operating systems fundamentals - B07

CS 150 Introduction to Computer Science 1

CMSC201 Computer Science I for Majors

CMPT 354: Database System I. Lecture 3. SQL Basics

G64DBS Database Systems. Lecture 7 SQL SELECT. The Data Dictionary. Data Dictionaries. Different Sections of SQL (DDL) Different Sections of SQL (DCL)

T-SQL Training: T-SQL for SQL Server for Developers

Constraints. Garbage in, garbage out

Lecture 2: Introduction to SQL

Develop Python Applications with MySQL Connector/Python DEV5957

WELCOME CSCA20 REVIEW SEMINAR

Sql Server Syllabus. Overview

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Administrivia. Administrivia. Faloutsos/Pavlo CMU /615

Lesson 14 Transcript: Triggers

Workshop. Introducing SQL: A Foundation of Data Analytics. Robb Sombach University of Alberta Alberta School of Business

pymonetdb Documentation

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

CMPS 181, Database Systems II, Final Exam, Spring 2016 Instructor: Shel Finkelstein. Student ID: UCSC

Steven Skiena. skiena

Lecture 1: Overview

CSC Web Programming. Introduction to SQL

This lecture. Basic Syntax

Access Intermediate

Essential SQLAlchemy. An Overview of SQLAlchemy. Rick Copeland Author, Essential SQLAlchemy Predictix, LLC

Informatics 1: Data & Analysis

Essential SQLite3. Section Title Page

CS122 Lecture 3 Winter Term,

Introduction to Database Systems CSE 414

Project 4 Query Optimization Part 2: Join Optimization

SQL-Nested Queries & Aggregate functions. Lecture By Binu Jasim 02-Aug-2016

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Wri$ng Django Extensions for PostgreSQL. Jonathan S. Katz Exco Ventures PGConf.eu 2011 Oct 20, 2011

Business Analytics. SQL PL SQL [Oracle 10 g] P r i n c e S e t h i w w w. x l m a c r o. w e b s. c o m

Outline for Today CSE 142. Programming a Teller Machine. CSE142 Wi03 I-1. ATM Algorithm for Dispensing Money

Declarative Programming

Computing for Medicine (C4M) Seminar 3: Databases. Michelle Craig Associate Professor, Teaching Stream

Now go to bash and type the command ls to list files. The unix command unzip <filename> unzips a file.

Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting

CS 2340 Objects and Design

CS 2316 Exam 3. Practice. Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

Chapter 18 Outputting Data

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

npm run pull npm start

Introduction to Database Systems CSE 414. Lecture 3: SQL Basics

In this Lecture. More SQL Data Definition. Deleting Tables. Creating Tables. ALTERing Columns. Changing Tables. More SQL

Android Programming Lecture 15 11/2/2011

2. Take a few minutes to look around the site. The goal is to familiarize yourself with a few key components of the NCBI.

Announcements (January 20) Relational Database Design. Database (schema) design. Entity-relationship (E/R) model. ODL (Object Definition Language)

Transcription:

COMP 364 - Lecture 25 March 26th, 2012 Mathieu Perreault Cherry blossoms of the Japanese Yoshino variety bloom along the Tidal Basin, March 19, 2012, in Washington, DC, with the Jefferson Memorial to the rear. This season celebrates the 100-year anniversary of the gift of the cherry trees from Japan to Washington, DC. (Paul J. Richards/AFP Getty Images) Python and SQLite

What we know so far Database is the main structure (only one database at a time) A database contains many tables, each with a column structure. e.g. gene_id, gene_name, gene_length for table genes A table can contain multiple records, each represented by a row. We can access the different columns of a record to get/set the data.

Database structure Database table1 table2 table3 column1, column2 column1,column2,column3 column1, column2 record1 <column1_value>, <column2_value> record1 <col1_value>, <col2_value>,<col3_value> record1 <column1_value>, <column2_value> record2 <column1_value>, <column2_value> record2 <col1_value>, <col2_value>,<col3_value> record2 <column1_value>, <column2_value> record3 <column1_value>, <column2_value>

Getting the content from a database Opening the database View the tables in a database View the fields (columns) in a table View all the records (rows) in a table View *some* of the records according to some filtering rule Full SQLite Syntax reference: http:// www.sqlite.org/lang.html

Creating new content in a database Opening the database Create a table in the database Insert records in the database Update records in the database Delete records according to a specific condition Delete tables Full SQLite Syntax reference: http://www.sqlite.org/lang.html

A word about PRIMARY KEY A primary key in a table ensures that the specified column will have a distinct value for each record Only one primary key is allowed per table. It could be an INTEGER, TEXT, etc. When specifying a column definition (such as when creating a table), just add PRIMARY KEY: CREATE TABLE grades (student_id INTEGER PRIMARY KEY, grade REAL); PRIMARY KEY AUTOINCREMENT will increase the key for you, no need to specifically insert it.

Default value You can also specify default values if you have special needs You simply need to add it to the column definition (for example, when creating a table) For example, specifying a default value of 100.0 CREATE TABLE grades (student_id INTEGER PRIMARY KEY, grade REAL DEFAULT 100.0);

Database structure my_database genes plates microarray gene_id, gene_name plate_id,date,humidity read_id, gene_id, amplitude record1 1, E.coli record1 50, 2012-03-16,0.65 record1 1, 1, 0.50 record2 2, BRCA record2 51, 2012-03-16,0.70 record2 2, 2, 0.65 record3 3, 2, 0.80

Joined at the hip (or: JOIN clauses) Sometimes you will have tables that complement other tables It s one advantage of SQLite, no need to store redudant information No need to store gene_name every time you make a measurement on the gene! To query for this information, we will need to tell SQLite that two fields from different tables are really talking about the same thing! Not sufficient to name them the same (e.g. gene_id)

Joined at the hip (or: JOIN clauses) The JOIN clause can be added to the source: SELECT [columns] FROM table1 JOIN table2 ON table1.field_foo=table2.field_bar WHERE [condition]; This says: I m selecting records in table 1, but bring in extra information about those records from table 2 The extra information can be used in the WHERE clause: SELECT * FROM genes JOIN microarray ON genes.gene_id=microarray.gene_id WHERE microarray.amplitude>0.6; Can augment the results: SELECT microarray.amplitude, genes.gene_name FROM genes JOIN microarray ON genes.gene_id=microarray.gene_id;

Today: SQLite in Python SQLite is included in Python (how convenient!) You can write code that interacts with SQLite. The power of Python, combined with SQLite, makes for a great program.

SQLite in Python Four things need to happen for Python to interact with SQLite Import the sqlite module import sqlite3 Connect to the database (or create one) with a given name connection = sqlite3.connect( database.sql ) Get a cursor to the database: cursor = connection.cursor() Execute queries on the cursor: cursor.execute( SELECT * FROM... )

SQLite in Python Once a query has been executed on the cursor, some data might be available, e.g. if you made a SELECT query. cursor.fetchone() will return one record at a time. cursor.fetchall() will return all matching records at once in a Python list. You have to test if the result is None before using it. Don t forget to call connection.commit() to commit the changes!