DSCS6020: SQLite and RSQLite
SQLite History SQlite is an open sorce 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 sed onboard gided missile systems = limited resorces. design goals of SQLite: operate program withot a database installation or administration. Users: Adobe Acrobat Reader, Apple, Mozilla, Google, PHP and Python SQLite is an in-process library that implements a self-contained, serverless, zero-configration, transactional SQL database engine.
Advantages Disadvantages Portable - Data is stored in a single cross-platform file, easily transferred Reliable 100% test coverage, open sorce code and bg database, ACID Small 300 kb library, rns in 16kb stack and 100kb heap SQLite is in the pblic domain transparent and free Does not reqire server administration High concrrency reader/writer locks on the entire file so only one ser 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 secrity. Limited data types and fnctions.
SQLite Uses Application file format transactions garantee ACID, triggers provide ndo/redo featre Temporary data analysis command line client, import CSV files and se SQL to analyze & generate reports Testing stand-in for enterprise DB dring application testing (limits potential damage!) Embedded devices small, reliable and portable Qery can reference mltiple database files http://www.sqlite.org
SQLite vs SQL "Lite" verison of SQL Spports most of the SQL syntax Only allow single writer at a time No ser management Best for mobile applications, testing NOT for big-scale data NOT for enterprises de to secrity reasons Spported data types: NULL, INTEGER, REAL, TEXT, BLOB
Data types in SQLite Version 3 NULL. The vale is a NULL vale. INTEGER. The vale is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitde of the vale. REAL. The vale is a floating point vale, stored as an 8-byte IEEE floating point nmber. TEXT. The vale is a text string, stored sing the database encoding (UTF-8, UTF-16BE or UTF-16LE). BLOB. The vale is a blob of data, stored exactly as it was inpt. Boolean and date/time data types are not spported
SQLite vs. SQL Featres
RSQLite The sorce incldes SQLite engine, database is stored in yor crrent working directory. Depends on DBI package, a database interface (DBI) for commnication between R and relational database management systems.
Fnctions Covered dbconnect() connect to a SQLite database dbsendqery() create a table and poplate dbwritetable() write a data frame or file database dblisttables() list tables in database dblistfields() list colmns in a table dbreadtable() fetch data from table dbremovetable() delete table from database dbgetqery() fetch data from database dbdisconnect() disconnect from SQLite database RSQLite docmentation: 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 ) Connects to the Test.sqlite database. If the database does not exist, it creates a new.sqlite file in the working directory. Database file mst end in.sqlite
Create table in database Use dbsendqery() fnction with the normal SQL CREATE TABLE command dbsendqery(conn = db, "CREATE TABLE School ( SchID INTEGER, Location TEXT, Athority TEXT, SchSize TEXT)") Atomatic 64 bit ROWID acts as primary key if not specified or WITHOUT ROWID is specified This step is not necessary if yo are directly importing a data frame or a file into the database.
Poplate table with SQL commands Use dbsendqery() fnction with SQL INSERT statement Insert data into the table one row at a time. dbsendqery(conn = db, "INSERT INTO School VALUES (1, 'rban', 'state', 'medim')") dbsendqery(conn = db, "INSERT INTO School VALUES ( 2, 'rban', 'independent', 'large')") dbsendqery(conn = db, "INSERT INTO School VALUES (3, 'rral', 'state', 'small')")
Poplate table with csv file or data frame object Use dbwritetable() fnction to insert entire contents of file or data frame into one table dbwritetable(conn = db, name = Stdent, vale = "stdent.csv, row.names = FALSE, header = TRUE) Use overwrite = TRUE or yo will have to remove the table if it already exists already. dbremovetable(db, Stdent")
Basic Fnctions dblisttables(db) - list tables in Database dblistfields(db, "School") list colmns in table dbreadtable(db, "School )- fetching data from table
Retrieve data from tables in the database dbgetqery() fetch data from database table dbgetqery(db, "SELECT * from School WHERE Location = rban ") First argment is the database name Second argment is jst the normal SQL SELECT statement in qotes Pt search vales in single qotes Can also se dbsendqery, dbfetch, then ClearReslt (bt more steps)
Close connection to the database dbdisconnect(db)
References https://www.sqlite.org https://cran.r-project.org/web/packages/rsqlite/rsqlite.pdf