Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL – Python and Databases (Continued)

Similar presentations


Presentation on theme: "SQL – Python and Databases (Continued)"— Presentation transcript:

1 SQL – Python and Databases (Continued)

2 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", ")", ";"]

3 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'", ")", ";"]

4 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

5 Integer Integers can have a negative sign in front of them, so if you have the tokens: [..., ",", "-", "23782", ","...] You should convert it to: [..., ",", "-23782", ",", ...]

6 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, [" "]

7 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) " " becomes (you can use the float function) "NULL" becomes None

8 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

9 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(): ...

10 Why is _ALL_DATABASES a dictionary?
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


Download ppt "SQL – Python and Databases (Continued)"

Similar presentations


Ads by Google