Download Link to Slides

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Corecursion wikipedia , lookup

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) ;
"
["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?
1.
2.
3.
4.
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