SQLite 1 CS440
What is SQLite? Open Source Database embedded in Android SQL syntax Requires small memory at runtime (250 Kbytes) Lightweight More info at: CS440 2
Types in SQLite TEXT (similar to String in Java) INTEGER (similar to long in Java) REAL (similar to double in Java) CS440 3
Getting started Download SQLite: Open a command prompt Create your first db: sqlite3 myDB.db create table tbl1(one varchar(10), two smallint); insert into tbl1 values('hello!',10); insert into tbl1 values('goodbye', 20); select * from tbl1; CS440 4
CREATE Create a new table to add your data Tables in databases are like excel spreadsheets CS440 5 CREATE TABLE employers ( _id INTEGER PRIMARY KEY, company_name TEXT); CREATE TABLE employees ( name TEXT, annual_salary REAL NOT NULL CHECK, employer_id REFERENCES employers(_id));
SQLite Types TEXT REAL BLOB INTEGER CS440 6
INSERT Adds a new data row CS440 7 INSERT INTO contacts(first_name) VALUES(“Thomas”); INSERT INTO employers VALUES(1, “Acme Balloons”); INSERT INTO employees VALUES(“Wile E. Coyote”, , 1);
SELECT Querying a database Returns one or multiple rows of results CS440 8 SELECT * FROM contacts; SELECT first_name, height_in_meters FROM contacts WHERE last_name = “Smith”; SELECT employees.name, employers.name FROM employees, employers WHERE employee.employer_id = employer._id ORDER BY employer.company_name ASC;
References cle.html#overview cle.html#overview CS440 9