Case Statements and Functions

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Structured Query Language - SQL Carol Wolf Computer Science.
Introduction to Python
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
The If/Else Statement, Boolean Flags, and Menus Page 180
Functions and abstraction Michael Ernst UW CSE 190p Summer 2012.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
1 Chapter 8 – Working with Databases spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Concepts of Database Management Seventh Edition
Introduction to Python
Munster Programming Training
FUNCTIONS AND STORED PROCEDURES & FUNCTIONS AND PROTECTING A DB AND PHP (Chapters 9, 15, 18)
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Input, Output, and Processing
CSC 2720 Building Web Applications Database and SQL.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Python Jim Eng Vote on class policy In lecture we discussed several ways in which you might show credits for content in the web pages.
1. 1. Which type of argument passes a value from a procedure to the calling program? A. VARCHAR2 B. BOOLEAN C. OUT D. IN 2.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏ Sec 9-7 Web Design.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1.
Oracle Data Integrator User Functions, Variables and Advanced Mappings
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
Overloading a constructor If a constructor is overloaded, then an if statement in the client is needed when creating the object. We have always declared.
Data Manipulation Variables, Data Types & Math. Aug Variables A variable is a name (identifier) that points to a value. They are useful to store.
1 Stored Procedures in MySQL Part I. 2 Objectives SQL Vs. MySQL SP MySQL SP Parameters MySQL SP Control Structures.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Cosc 5/4730 Sqlite primer.
SQL – Python and Databases
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Adapters and Converters
Stored Procedures.
Aggregate Functions and Collations
Introduction to Python
© 2010 David A Watt, University of Glasgow
SQL – Data types.
Understand Computer Storage and Data Types
SQL – Parameterized Queries
CPSC-310 Database Systems
CS122B: Projects in Databases and Web Applications Spring 2017
CS122B: Projects in Databases and Web Applications Winter 2017
SQL – Python and Databases (Continued)
CIS 136 Building Mobile Apps
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Logical Operations In Matlab.
Coding Concepts (Data- Types)
Variables, Data Types & Math
Variables, Data Types & Math
Chapter 8 Advanced SQL.
Information Management
Variables, Data Types & Math
COMPUTER PROGRAMMING SKILLS
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.
CISC101 Reminders Assignment 3 due today.
Class code for pythonroom.com cchsp2cs
JDBC II IS
Presentation transcript:

Case Statements and Functions

CASE Statement CASE WHEN grade < 2.0 THEN SET status = 'Failing'; WHEN grade <= 3.5 THEN SET status = 'Passing'; ELSE SET status = 'Rocking!'; END CASE; CASE statement are used for conditional expression. There are two forms, the first (shown above) tests until a WHEN clause is true, the THEN clause is executed.

CASE Statement (Alternate form) CASE status WHEN 'Failing' THEN SET come_talk = 1; WHEN 'Passing' THEN SET come_talk = 0; ELSE SET come_talk = 0; END CASE; This is the second form of a CASE STATEMENT. The expression between CASE and WHEN is evaluated, and each WHEN clause is tests for equality. If equal, the THEN clause is executed.

CASE Expression You can also use CASE as an expression. UPDATE students SET come_talk = CASE WHEN grade < 2.0 THEN 1; WHEN grade > 4.0 THEN 2; ELSE 0; END CASE; ;

CASE Expression (Alternate Form) UPDATE students SET come_talk = CASE status WHEN 'Passing' THEN 1; WHEN 'Rocking!' THEN 2; ELSE 0; END CASE; ;

CASE statements and expressions You can only use CASE statements in stored procedures. But you can use CASE expressions anywhere you can use a value. So, SQLite only supports CASE expressions, not CASE statements.

Where can you use variables and CASE statements? In any legal SQL statement Only in stored procedures Only in stored procedures and triggers I can use them anywhere, they just might not work

Functions We've seen functions in SQL before (e.g. "date"), they take 0 or more arguments and return one result. Here are some more: abs(x) - returns the absolute value of its argument lower(x) - returns a copy of a string in lower case upper(x) - guess what this does random(x) - returns an random, signed 64-bit integer typeof(x) - returns the type ("null", "integer", "real", "text", "blob") of x round(x, y) - returns a floating-point value x rounds to y digits after the decimal point trim(x, y) - returns a copy of string x with y characters removed from each end

User Defined Functions What if we wanted to add a custom function to SQLite? title(x) - returns the string x in "Title Case" (the first letter of each word is capitalized). We first write a function (in Python) that does what we want: def make_title(x): return x.title() Then we need to tell our connection to the database that this function exists: conn.create_function("title", 1, make_title) Now we can use it in SQL queries: conn.execute("SELECT title(name) FROM students;")

create_function The create_function method on the connection object takes three parameters: name - a string giving the name the function will be called by in SQL num_params - the number of parameters the function requires You can overload functions with different number of arguments func - the function (in Python) to be called The function can return any of the SQLite supported types (bytes, str, int, float, and None)

Functions versus Stored Procedures Functions must return a value, stored procedures can "return" 0 or more values. Functions only have "in" arguments, whereas stored procedures can have "in", "out" and "inout" arguments. Stored procedures can manipulate the database directly (SELECT, UPDATE, INSERT, DELETE), where functions can only return a value.