
Day 34 Databases CSE115 Introduction to Computer Science I WEEK 12 This week Next week UB Infinite Should have completed up to: Custom Sorting (PY and JS) This week work on: Databases Lab activities Lab Activity 07 UB Infinite prerequisite (enforced!): Custom Sorting (PY and JS) Lab exams Lab Exam 4 Databases Lab Exam 3 make-up Must have taken lab exam 3 Recitation participation Recitation Activity 10 TBD Lab attendance Required at specific time Post private questions in Piazza (see MyGrades in UBLearns) Lecture responses Every recording (questions embedded in recordings) Must complete by day of lecture Lecture activities Wed 04/21 Wed 04/28 ONLY Fri 04/23 (no activity on Fri 04/30) Programming project Project due Friday Work on project Apr 30 @ 6:00 PM Final exam Nothing for now. During official final exam period Keep good notes to study from! (check HUB for date/time) WEEK 12 Lecture plan MONDAY 04/19 WEDNESDAY 04/21 FRIDAY 04/23 Pre-recorded topic: Pre-recorded topic: Pre-recorded topic: Databases Database application SQL injection Lecture Activity Lecture Activity (for credit) (for credit) @ 9:10 AM – 10:00 AM @ 9:10 AM – 10:00 AM via Zoom via Zoom Lecture Goals • Describe why database used for long-term storage • Contrast strengths of database relative to text & CSV files • Explain why database file is not readable on its own • Describe how a database organizes data using tables • Write Python database code using SQLite3 library • State steps needed to start and eventually complete a session • Show how cursor used to execute SQL commands • Write SQL commands which create a table, add data to a table, and retrieve data from a table • Describe how to use results of executing SELECT statement Today's Plan Database basic Database commands Comprehension question Persisting data Central Random Processing Access Unit Memory CPU RAM persistent storage (e.g. file or database) Ways to Persist Data Text file Stored as stream of characters CSV file Stored as comma-separated fields Database Stored for highly-efficient data operations SQLite SQLite is an in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. The code for SQLite is in the public domain and is thus free for use for any purpose, commercial or private. SQLite is the most widely deployed database in the world with more applications than we can count, including several high-profile projects. From https://www.sqlite.org/about.html SQLite SQLite is library that implements a database engine. From https://www.sqlite.org/about.html Using SQLite import sqlite3 conn = sqlite3.connect('Need importatest.db') before using library cur = conn.cursor() # Create/Read/Update/Delete things in database conn.commit() conn.close() Using SQLite import sqlite3 conn = sqlite3.connect('file.db') cur = conn.cursor() If file named file.db exists, # Create/Read/Update/Deleteopen it things as a databasein database conn.commit() If file does not exist, conn.close() create it as a database Using SQLite import sqlite3 conn = sqlite3.connect('file.db') cur = conn.cursorFile() format optimized # Create/Read/Update/Deletefor data things processing. in database It is not human-readable conn.commit() conn.close() Using SQLite import sqlite3 conn = sqlite3.connect('file.db') cur = conn.cursor() connect()# Create/Read/Update/Deletereturns value things in database needed to work with conn.commit() database conn.close() Using SQLite import sqlite3 conn = sqlite3.connect('file.db') cur = conn.cursor() # Create/Read/Update/Delete things in database conn.commitNeed database's() cursor to run commands conn.close() Using SQLite import sqlite3 conn = sqlite3.connect('file.db') cur = conn.cursor() # Create/Read/Update/Delete things in database conn.commit() Can now use cursor (cur) conn.close() to perform actions with DB Using SQLite import sqlite3 conn = sqlite3.connect('file.db') cur = conn.cursor() # Create/Read/Update/Delete things in database conn.commit() conn.close() Must commit() changes to save them Using SQLite import sqlite3 conn = sqlite3.connect('file.db') cur = conn.cursor() # Create/Read/Update/Delete things in database conn.commit() Terminate session conn.close() to ensure saving Using DB Key Concept Make certain Python library is imported import sqlite3 Connect to sqlite3 DB file connection = sqlite3.connect(DBfilename) Get cursor for database cursor = connection.cursor() Create, Read, Update, & Delete data from database Save changes and clean up processes connection.commit() connection.close() Today's Plan Database basic Database commands Comprehension question Executing SQL Commands Key Concept Open the database connection Get a cursor for the database Write SQL statement as argument to execute() cursor.execute('SQL to run') SQL: Create a New Table CREATE TABLE IF NOT EXISTS name columns name - Name of table to create in database columns - Names of columns to create in table SQL Example: CREATE TABLE IF NOT EXISTS movies (title,year) SQL: Create a New Table CREATE TABLE IF NOT EXISTS name columns name - Name of table to create in database columns - Names of columns to create in table Example using SQLite cursor (cur): cur.execute( ) Call cursor's execute()function SQL: Create a New Table CREATE TABLE IF NOT EXISTS name columns name - Name of table to create in database columns - Names of columns to create in table Example using SQLite cursor (cur): cur.execute( \ 'CREATE TABLE IF NOT EXISTS ' + \ 'movies (title,year)') Argument is SQL command SQL: Add Row Into Table INSERT INTO table VALUES(x,y,z) table - Name of table where row is added (x,y,z) – Row's values for columns in table SQL Example: INSERT INTO movies VALUES ("Jaws", 1975) SQL: Add Row Into Table INSERT INTO table VALUES(x,y,z) table - Name of table where row is added (x,y,z) – Row's values for columns in table Example using SQLite cursor (cur): cur.execute( \ 'INSERT INTO movies VALUES' + \ '("Jaws", 1975)') SQL: Add Row Into Table INSERT INTO table VALUES(x,y,z) table - Name of table where row is added (x,y,z) – Row's values for columns in table Example using SQLite cursor (cur): "Jaws" Text values MUST be in quotes SQL: Read All Rows SELECT * FROM table table - Name of table to return rows from SQL Example: SELECT * FROM movies SQL: Read All Rows SELECT * FROM table table - Name of table to return rows from Example using SQLite cursor (cur): cur.execute('SELECT * FROM movies') SQL: Read All Rows SELECT * FROM table table - Name of table to return rows from Example using SQLite cursor (cur): cur.execute('SELECT ') When SELECT run, execute() returns object used to loop over rows Getting DB Data Key Concept Open the database connection Get a cursor for the database Write SQL statement SELECTing data rows = cursor.execute('SELECT…') Get row as list from rows using for..in for row in rows : SQL: Read Some Rows SELECT * FROM table WHERE constraint table - Name of table to return rows from constraint - Expression used to match rows SQL Example: SELECT * FROM movies WHERE year > 2000 SQL: Read Some Rows SELECT * FROM table WHERE constraint table - Name of table to return rows from constraint - Expression used to match rows Example using SQLite cursor (cur): cur.execute('SELECT * FROM ' + \ 'movies WHERE year > 2000') SQL: Read Some Rows SELECT * FROM table WHERE constraint table - Name of table to return rows from constraint - Expression used to match rows Example using SQLite cursor (cur): cur.execute('SELECT Still a SELECT so execute() works same as before Key SQL Commands Example commands CREATE Table in Database CREATE TABLE IF NOT EXISTS movies (title,year) INSERT Row into a Table INSERT INTO movies VALUES ("Jaws", 1975) SELECT All Rows in a Table SELECT * FROM movies SELECT Some Rows in a Table SELECT * FROM movies WHERE year > 2000 Key SQL Commands Executing SELECT DOES NOT return a list SELECT All Rows in a Table SELECT * FROM movies SELECT Some Rows in a Table SELECT * FROM movies WHERE year > 2000 Today's Plan Database basic Database commands Comprehension question.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages36 Page
-
File Size-