SQL – Python and Databases

Slides:



Advertisements
Similar presentations
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Advertisements

Database Systems More SQL Database Design -- More SQL1.
SQLLite and Java CS-328 Dick Steflik. SQLLite Embedded RDBMS ACID Compliant Size – about 257 Kbytes Not a client/server architecture –Accessed via function.
SQLite 1 CS440. What is SQLite?  Open Source Database embedded in Android  SQL syntax  Requires small memory at runtime (250 Kbytes)  Lightweight.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Data Storage: Part 4 (Content Providers). Content Providers Content providers allow the sharing of data between applications. Inter-process communication.
COMP 365 Android Development.  Manages access from a central database  Allows multiple applications to access the same data.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
M1G Introduction to Database Development 2. Creating a Database.
Stored Procedure. Objective At the end of the session you will be able to know :  What are Stored Procedures?  Create a Stored Procedure  Execute a.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Introduction to Computing Using Python Data Storage and Processing  How many of you have taken IT 240?  Databases and Structured Query Language  Python.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1.
SQlite. SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 6: Accessing a database with PHP Rob Gleasure robgleasure.com.
Jennifer Widom Relational Databases The Relational Model.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Introduction to Database Programming with Python Gary Stewart
Introduction to pysqlite
Creating Database Objects
CPSC-310 Database Systems
Cosc 5/4730 Sqlite primer.
SQL – join.
Databases.
Adapters and Converters
Aggregate Functions and Collations
SQL – CRUD.
Android Application SQLite 1.
Indices.
Computer Programming BCT 1113
Containers and Lists CIS 40 – Introduction to Programming in Python
SQL – More Table Constraints
IS444: Modern tools for applications development
SQL – Data types.
Case Statements and Functions
Python’s input and output
Formatting Output.
IS444: Modern tools for applications development
CS 440 Database Management Systems
SQL – Application Persistence Design Patterns
ISC440: Web Programming 2 Server-side Scripting PHP 3
SQL – Parameterized Queries
SQL Standard Query Language Good for manipulating a Database
SQL – Python and Databases (Continued)
Topics Introduction to File Input and Output
Relational Databases The Relational Model.
Relational Databases The Relational Model.
SQL data definition using Oracle
CIS 136 Building Mobile Apps
Recitation Outline C++ STL associative containers Examples
CREATE, INSERT, SELECT.
CMPT 354: Database System I
Implementation of Relational Operations
Topics Introduction to File Input and Output
CMSC-461 Database Management Systems
Topics Introduction to File Input and Output
Mobile Programming Dr. Mohsin Ali Memon.
SQL – Application Persistence Design Patterns
Creating Database Objects
Database Instructor: Bei Kang.
JDBC II IS
Presentation transcript:

SQL – Python and Databases

How to represent a database in python Project 2 Requirement: Needs to match the SQLite input and output identically (for the subset of SQL used in the projects) The rest of the implementation content is entirely just suggestions on one way to solve the problem.

SQLite to Python SQLite type Python type NULL None INTEGER int REAL float TEXT str BLOB bytes You can ignore the existence of BLOBs for the projects

Type Checking Most databases require that if a column is declared to have a type, each element in that column must conform or be converted. SQLite doesn't mind mismatched types. It won't matter for this course as all SQL in tests will not have type mismatches Example of a type mismatch: CREATE TABLE x (name TEXT); INSERT INTO x VALUES (5);

Rows You can represent a row in a table as a python tuple: INSERT INTO x VALUES (1, 1.0, 'I''m a string', NULL); (1, 1.0, "I'm a string", None) If a row has only a single value, make sure it is still in a tuple: INSERT INTO x VALUES ('One is the loneliest number'); ("One is the loneliest number",) NOT ("One is the loneliest number")

Table A table can be represented by a class in python with two parts: The schema (table name, column names and types) The rows Rows aren't necessary unique, and row order doesn't matter, but you need to be able to iterate over the rows to get their content. You need to be able to add and remove rows easily

What data structure could hold the rows of a table? set list bag (multiset) tuple

Database A database can contain 0 or more tables. It needs to be able to find tables by their name. You can use class that contains a dictionary, mapping the tables' names to the table instances Can a table exist outside of a database? No

Project API Like python's sqlite3 module: import project conn = project.connect("filename.db") conn.execute("CREATE TABLE myTable (name TEXT);") conn.execute("INSERT INTO myTable VALUES ('Josh');") rows = conn.execute("SELECT * FROM myTable;") for row in rows: print(row)

connect connect is a module function that takes a filename and results a connection object The filename will be used in later projects that need your database to write its data to a file. For project 2, it should be ignored. The object returned should be able to access the database to pass queries and return results with an execute method.

Execute The execute method on the Connection object takes a string (query) and returns an iterable object or None, depending on if the query returns rows from the database. One possible implementation: Do the query, then If not a SELECT query, return None Otherwise return a list of rows (tuples), may be empty if no rows exist

How to write the execute method? Each query is guaranteed to be a legal SQL statement, for Project 2 there are only a few statements to handle: CREATE TABLE ... INSERT INTO ... SELECT ... Caveat: whitespace is allowed before, after, and within queries Spaces, \n, \t, \r