Top Banner
DSCS6020: SQLite and RSQLite
17

DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

Jul 09, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

DSCS6020: SQLite and RSQLite

Page 2: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

SQLite History• SQlite is an open source embedded database, meaning that it doesn’t

have a separate server process.• Reads and writes to ordinary disk files.• The original implementation was designed by D. Richard Hipp, who was

designing software used onboard guided missile systems = limited resources.• design goals of SQLite: operate program without a database installation or

administration. u Users: Adobe Acrobat Reader, Apple, Mozilla, Google, PHP and Pythonu “SQLite is an in-process library that implements a self-contained, serverless,

zero-configuration, transactional SQL database engine.”

Page 3: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

Advantages

• Portable - Data is stored in a single cross-platform file, easily transferred

• Reliable – 100% test coverage, open source code and bug database, ACID

• Small – 300 kb library, runs in 16kb stack and 100kb heap

• SQLite is in the public domain –transparent and free

• Does not require server administration

Disadvantages

• High concurrency – reader/writer locks on the entire file so only one user can edit at a time

• Limited size – DB file can’t exceed file system limit or 2TB

• No access control – no way to set permissions, so no security.

• Limited data types and functions.

Page 4: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

SQLite Uses

u Application file format – transactions guarantee ACID, triggers provide undo/redo feature

u Temporary data analysis – command line client, import CSV files and use SQL to analyze & generate reports

u Testing – stand-in for enterprise DB during application testing (limits potential damage!)

u Embedded devices – small, reliable and portableu Query can reference multiple database filesu http://www.sqlite.org

Page 5: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

SQLite vs SQLu "Lite" verison of SQLu Supports most of the SQL syntaxu Only allow single writer at a timeu No user managementu Best for mobile applications, testingu NOT for big-scale datau NOT for enterprises due to security reasonsu Supported data types: NULL, INTEGER, REAL, TEXT, BLOB

Page 6: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

Data types in SQLite Version 3

u NULL. The value is a NULL value.u INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8

bytes depending on the magnitude of the value.u REAL. The value is a floating point value, stored as an 8-byte IEEE

floating point number.u TEXT. The value is a text string, stored using the database encoding

(UTF-8, UTF-16BE or UTF-16LE).u BLOB. The value is a blob of data, stored exactly as it was input.u Boolean and date/time data types are not supported

Page 7: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

SQLite vs. SQL Features

Page 8: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

RSQLiteu The source includes SQLite engine, database is stored in your current

working directory.u Depends on DBI package, a database interface (DBI) for communication

between R and relational database management systems.

Page 9: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

Functions Covered

u dbConnect() – connect to a SQLite databaseu dbSendQuery() – create a table and populateu dbWriteTable() – write a data frame or file databaseu dbListTables() – list tables in databaseu dbListFields() – list columns in a tableu dbReadTable() – fetch data from tableu dbRemoveTable() – delete table from database u dbGetQuery() – fetch data from databaseu dbDisconnect() – disconnect from SQLite databaseu RSQLite documentation:

u https://cran.r-project.org/web/packages/RSQLite/RSQLite.pdf

Page 10: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

Install package and connect to database

Install.packages(RSQLite, dependencies = TRUE)

db <- dbConnect(SQLite(), dbname=“Test.sqlite”)

u Connects to the Test.sqlite database. If the database does not exist, it creates a new .sqlite file in the working directory.

u Database file must end in .sqlite

Page 11: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

Create table in database

u Use dbSendQuery() function with the normal SQL CREATE TABLE command

dbSendQuery(conn = db, "CREATE TABLE School (SchID INTEGER,Location TEXT, Authority TEXT, SchSize TEXT)")

u Automatic 64 bit ROWID acts as primary key if not specified or WITHOUT ROWID is specified

u This step is not necessary if you are directly importing a data frame or a file into the database.

Page 12: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

Populate table with SQL commandsu Use dbSendQuery() function with SQL INSERT statementu Insert data into the table one row at a time.

dbSendQuery(conn = db, "INSERT INTO School VALUES (1, 'urban', 'state', 'medium')")

dbSendQuery(conn = db, "INSERT INTO School VALUES (2, 'urban', 'independent', 'large')")

dbSendQuery(conn = db, "INSERT INTO School VALUES (3, 'rural', 'state', 'small')")

Page 13: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

Populate table with csv file or data frame object

u Use dbWriteTable() function to insert entire contents of file or data frame into one table

dbWriteTable(conn = db, name = ”Student”, value = "student.csv”, row.names = FALSE, header = TRUE)

u Use overwrite = TRUE or you will have to remove the table if it already exists already.

dbRemoveTable(db, ”Student")

Page 14: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

Basic Functions

u dbListTables(db) - list tables in Databaseu dbListFields(db, "School") – list columns in tableu dbReadTable(db, "School”)- fetching data from table

Page 15: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

Retrieve data from tables in the database

u dbGetQuery() – fetch data from database table

dbGetQuery(db, "SELECT * from School WHERE Location = ‘urban’")

u First argument is the database nameu Second argument is just the normal SQL SELECT statement in quotesu Put search values in single quotesu Can also use dbSendQuery, dbFetch, then ClearResult (but more

steps)

Page 16: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

Close connection to the database

u dbDisconnect(db)

Page 17: DSCS6020: SQLite and RSQLiteis2000.weebly.com/uploads/8/6/5/9/8659576/rsqlite.pdf · SQLite History • SQlite is an open source embedded database, meaning that it doesn’t have

References

u https://www.sqlite.orgu https://cran.r-project.org/web/packages/RSQLite/RSQLite.pdf