sqlite - new mexico institute of mining andcse373/hw/sqlite.pdf · android and ios email readers...
TRANSCRIPT
C S E 3 7 3
SQLite
Outline
� Introduction to SQL
� SQLite
� SQLite Shell
� SQLite From A Program� SQLite From A Program
� An example in Java
� Conclusions
SQL
Quick Introduction To SQL
� The Structured Query Language (SQL) is a programming language designed to interact with relational databases� The SQL anagram can be pronounced either S-Q-L or sequel
� SQL is designed to work with databases that are composed of tables
The columns are data typed fields� The columns are data typed fields
� The rows are instances of data
� Relationships can be established between the columns of tables
� The formal description of the names and structure of the tables in a database is called a schema� Often the schema is the series of SQL statements that create the tables,
relationships and constraints
Quick Introduction To SQL
� There are four basic functions of any storage system (CRUD)� Create
� Read
� Update
� Delete
� The statements in SQL that support CRUD are:� The statements in SQL that support CRUD are:� CREATE TABLE - Create
� INSERT - Create
� SELECT - Read
� UPDATE – Update
� ALTER TABLE - Update
� DELETE – Delete
� DROP - Delete
SQL – CREATE TABLE
� The CREATE TABLE statement is used in SQL to create a
new table in a database
CREATE TABLE My_table(
my_field1 INT,
my_field2 VARCHAR(50),
my_field3 DATE NOT NULL,
PRIMARY KEY (my_field1, my_field2)
);
SQL – Primary And Foreign Keys
� Primary keys are a constraint on a table that specifies that the
columns (or column) that are specified as the primary key
must have a unique combination for each row in the table
� Usually we are using the primary key as the identifying information for a
row. For example {Student ID}, {SSN}, {Name, Date of Birth, Phone number}
can all be primary keys that would identify a person in a databasecan all be primary keys that would identify a person in a database
� Foreign keys are relationships between the columns of two
tables that act as constraint stating that the combination of
the values of the columns in the foreign key must exist in the
primary key of another table
� Usually this is used to state the row in the table with the foreign key is the
same entity as the row in the table with the primary key
SQL - INSERT
� The INSERT statement is add rows to the a table in a
database
INSERT INTO My_table
(field1, field2, field3)
VALUES
('test', 'N', 8/21/2013);
SQL - SELECT
� The SELECT statement is used to send query to a database
and obtained results for the query
SELECT * FROM My_table;
SELECT (my_field1, my_field3)
FROM My_table
WHERE
my_field1 > 12
AND myField2 = ‘Bob’;
SQL - UPDATE
� The UPDATE statement is used to change the value of
field in a row in the database
UPDATE My_table
SET field1 = 0
WHERE field2 = 'N';
SQL – ALTER TABLE
� The ALTER TABLE statement is used to change the
structure of a table
ALTER TABLE My_table
ADD my_field4 NUMBER(3) NOT NULL;
ALTER TABLE My_table
DROP my_field1;
SQL - DELETE
� The DELETE statement is used to delete rows from table in the database
DELETE FROM My_table
WHERE field2 = 'N';WHERE field2 = 'N';
� The TRUNCATE TABLE statement is used to quickly delete all data in a table
TRUNCATE TABLE My_table;
SQL - DROP
� The DROP statement is used to delete a table from the
database
DROP TABLE My_table;
More SQL
� SQL is a fairly large language and has a lot features and a
lot of syntax
� Some important features that weren’t covered include
� Procedures
� Constraints� Constraints
� Permissions
� This was just meant as a quick crash course to provide the
bare bones of what is needed to go over a pretty neat
database system
SQLite
Database Systems
� There are a number of database systems that you use
including mySQL, Oracle, SQLServer, and PostgreSQL
� Most of these databases are designed to be running all
the time and run as a separate process
What if we don’t want or need the database to be � What if we don’t want or need the database to be
running all the time? Or what if we are only storing data
for a single user in a single application ?
SQLite
� SQLite is a very lightweight C library that allows SQL
queries to sent to a local flat file
� Can be pronounced either S-Q-L-lite or Sequel-lite
� Additionally, since SQLite is a library it doesn’t run as a
separate process but rather is invoked from another separate process but rather is invoked from another
program
� SQLite is accessible from most programming
languages(maybe all)
� The source code for SQLite is in the public domain and so
it is viewable and modifiable
Uses Of SQLite
� Arguably SQLite is the most commonly used database in
the world
� Most SQLite users aren’t aware they are using it
� Firefox, Chrome, Safari and other web browsers
Store bookmarks, settings, browsing history� Store bookmarks, settings, browsing history
� Android and iOS
� Email Readers
� Emails, Contact Lists
� Microsoft has a similar system that it uses for the same
purposes
SQL In SQLite
� At this point SQLite supports almost all of SQL
� There are some missing features in joins, altering tables,
triggers, and supporting permissions
� In my experience there is also some places where SQLite
can be somewhat strangecan be somewhat strange
� SQLite wont enforce foreign keys unless you tell it to every time you
access the database
� The documentation for SQLite is pretty comprehensive
� http://www.sqlite.org/docs.html
SQLite Datatypes
� SQLite only has five datatypes
� NULL – For null values
� INTEGER – A 1 to 8 byte signed integer
� REAL – An 8byte floating point values
� TEXT – A text string
� BLOB – Data that stored in such a way that it is unchanged for how it was input
� Any other datatype you want to support needs to modified to fit into one of these datatypes
� Boolean – Usually an integer is used
� Date/Time – Built-in date/time functions can be used to store them in TEXT, REAL or INTEGER formats
The SQLite Shell
SQLite Shell
� SQLite provides an executable command line shell that
can be used to create and interact with a SQLite database
directly
� The pre-compiled shell can be obtained off the
downloads on the SQLite homepage for Windows, Linux downloads on the SQLite homepage for Windows, Linux
and OS X
� http://www.sqlite.org/download.html
� The shell is portable and so doesn’t require any
installation or installed libraries
SQLite Shell
� There are several commands available from within the
shell including:
� .databases – lists all the currently loaded databases
� .schema – lists the schema for the database(s)
� .tables – lists the names of all tables
� .read – will execute the SQL in the specified file
� .echo – repeats the commands inputted
� .quit – quits the shell
� .help – lists all commands
SQLite Data Browser
� Creating a new SQLite database is as easy as creating a new file� SQLite doesn’t enforce any requirements on the file extension for a SQLite
database
� Popular file extensions include .db and .sqlite3 or no extension at all
� Opening a SQLite database with the shell involves providing � Opening a SQLite database with the shell involves providing the file name as a command line argument� sqlite3 mydb.sqlite
� If you are already in the shell you can use:
� attach “mydb.sqlite” as db1;
� Once you are in the shell you can then start entering and executing SQL queries and statements
SQLite From A Program
SQLite From A Program
� SQLite provides a C/C++ as well as a Tcl API
� There are numerous bindings that allow SQLite to be used
from just about any programming language
� Java
Python� Python
� .Net
� Javascript
� The SQLite wiki maintains a list of bindings by language
� http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers
SQLite From A Program
� I’m going to go over a simple example of how to use Java
to interact with a SQLite database
� I used a simple JDBC driver to interface with SQLite
� https://bitbucket.org/xerial/sqlite-jdbc
There are other ways to use SQLite from Java� There are other ways to use SQLite from Java
Installing The Driver In Eclipse
� First we download the .jar file for the driver
� “sqlite-jdbc-3.7.2-javadoc.jar”
� Place the .jar into a directory named “lib” in the project’s
directory
Refresh the project in Eclipse� Refresh the project in Eclipse
� Right click on the project in the package explorer
� Now we are ready to start using SQLite
Using SQLite
� Lets take a walk through the code for a simple example
Conclusions
� SQLite is a very simple and easy to use database system
� It allows for very portable databases and drops the
requirement of a database system running around the
clock
It can be accessed from just about any programming � It can be accessed from just about any programming
language or just from a command line shell
Questions?