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

Post on 09-Jul-2020

15 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

DSCS6020: SQLite and RSQLite

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.”

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.

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

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

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

SQLite vs. SQL Features

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.

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

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

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.

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')")

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")

Basic Functions

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

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)

Close connection to the database

u dbDisconnect(db)

References

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

top related