SQLite: Difference between revisions
No edit summary |
|||
(13 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
<languages /> | |||
[https://www.sqlite.org SQLite] is a | <translate> | ||
<!--T:1--> | |||
[https://www.sqlite.org SQLite] is a database management tool used to build commonly called ''pocket databases'' because they offer all the features of relational databases without the client-server architecture and with the added advantage of the data residing on a single disk file which can simply be copied to another computer. Software written in a wide variety of common languages can read from and write to the database file with standard [https://en.wikipedia.org/wiki/SQL SQL] queries using the language's API for database interactions. | |||
<!--T:2--> | |||
Like any other database, an SQLlite database should not be used on a shared filesystem such as home, scratch and project. Typically, you should copy your SQLite file to the local scratch <tt>$SLURM_TMPDIR</tt> space at the beginning of a job and you can then use the database without any issues and also enjoy the best possible performance. Note that SQLite is not intended for use with multiple threads or processes writing concurrently to the database; for this you should consider a [[Database_servers | client-server solution]]. | |||
==Using SQLite directly== <!--T:3--> | |||
<!--T:4--> | |||
You can access an SQLite database directly using the native client: | You can access an SQLite database directly using the native client: | ||
{{Command|sqlite3 foo.sqlite}} | {{Command|sqlite3 foo.sqlite}} | ||
If the file <tt>foo.sqlite</tt> does not already exist, | If the file <tt>foo.sqlite</tt> does not already exist, SQLite will create it and the client will start in an empty database, otherwise you will be connected to the existing database. You may then execute whichever queries you wish on the database, such as <tt>SELECT * FROM tablename;</tt> to print to the screen the entire contents of the table <tt>tablename</tt>. | ||
==Accessing SQLite from software== <!--T:5--> | |||
<!--T:6--> | |||
The most common way to interact with an SQLite (or other) database is through function calls to open a connection to the database; execute queries that can read, insert or update existing data; and close the connection to the SQLite database so that any changes are flushed to the SQLite file. In the simple example below, we suppose that the database has already been created with a table called <tt>employee</tt> that has two columns: the string <tt>name</tt> and the integer <tt>age</tt>. | |||
</translate> | |||
<tabs> | |||
<tab name="Python"> | |||
<source lang="python"> | |||
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||
# For Python we can use the module sqlite3, installed in a virtual environment, | |||
# to access an SQLite database | |||
import sqlite3 | import sqlite3 | ||
age = 34 | |||
# Connect to the database... | |||
dbase = sqlite3.connect("foo.sqlite") | dbase = sqlite3.connect("foo.sqlite") | ||
dbase.execute("INSERT INTO employee(name,age) VALUES(\"John Smith\"," + str(age) + ");") | |||
# Close the database connection | |||
dbase.close() | dbase.close() | ||
</source> | |||
== | </tab> | ||
<tab name="R"> | |||
<source lang="r"> | |||
# Using R, the first step is to install the RSQLite package in your R environment, | |||
# after which you can use code like the following to interact with the SQLite database | |||
library(DBI) | |||
age <- 34 | |||
# Connect to the database... | # Connect to the database... | ||
dbase <- dbConnect(RSQLite::SQLite(),"foo.sqlite") | dbase <- dbConnect(RSQLite::SQLite(),"foo.sqlite") | ||
# A parameterized query | # A parameterized query | ||
query <- paste(c("INSERT INTO employee(name,age) VALUES(\"John Smith\",",toString(age),");"),collapse='') | |||
dbExecute(dbase,query) | |||
# Close the database connection | # Close the database connection | ||
dbDisconnect(dbase) | dbDisconnect(dbase) | ||
</source> | </source> | ||
</tab> | </tab> | ||
<tab name="C++"> | <tab name="C++"> | ||
<source lang="cpp"> | <source lang="cpp"> | ||
#include <iostream> | |||
#include <string> | #include <string> | ||
#include <sqlite3.h> | #include <sqlite3.h> | ||
Line 81: | Line 69: | ||
int main(int argc,char** argv) | int main(int argc,char** argv) | ||
{ | { | ||
int age = 34; | |||
std::string query; | std::string query; | ||
sqlite3* dbase; | sqlite3* dbase; | ||
sqlite3_open("foo.sqlite",&dbase); | sqlite3_open("foo.sqlite",&dbase); | ||
query = " | |||
query = "INSERT INTO employee(name,age) VALUES(\"John Smith\"," + std::to_string(age) + ");"; | |||
sqlite3_exec(dbase,query.c_str(),nullptr,nullptr,nullptr); | sqlite3_exec(dbase,query.c_str(),nullptr,nullptr,nullptr); | ||
sqlite3_close(dbase); | sqlite3_close(dbase); | ||
Line 94: | Line 85: | ||
</tab> | </tab> | ||
</tabs> | </tabs> | ||
<translate> | |||
==Limitations== <!--T:7--> | |||
<!--T:8--> | |||
As its name suggests, SQLite is easy to use and intended for relatively simple databases which are neither excessively large (hundreds of gigabytes or more) nor too complicated in terms of their [https://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model entity-relationship diagram]. As your SQLite database grows in size and complexity, the performance could start to degrade, in which case the time may have come to consider the use of more [[Database_servers | sophisticated database software which uses a client-server model]]. The SQLite web site includes an excellent page on [https://www.sqlite.org/whentouse.html Appropriate Uses For SQLite], including a checklist for choosing between SQLite and client-server databases. | |||
</translate> |
Latest revision as of 19:47, 12 March 2020
SQLite is a database management tool used to build commonly called pocket databases because they offer all the features of relational databases without the client-server architecture and with the added advantage of the data residing on a single disk file which can simply be copied to another computer. Software written in a wide variety of common languages can read from and write to the database file with standard SQL queries using the language's API for database interactions.
Like any other database, an SQLlite database should not be used on a shared filesystem such as home, scratch and project. Typically, you should copy your SQLite file to the local scratch $SLURM_TMPDIR space at the beginning of a job and you can then use the database without any issues and also enjoy the best possible performance. Note that SQLite is not intended for use with multiple threads or processes writing concurrently to the database; for this you should consider a client-server solution.
Using SQLite directly
You can access an SQLite database directly using the native client:
[name@server ~]$ sqlite3 foo.sqlite
If the file foo.sqlite does not already exist, SQLite will create it and the client will start in an empty database, otherwise you will be connected to the existing database. You may then execute whichever queries you wish on the database, such as SELECT * FROM tablename; to print to the screen the entire contents of the table tablename.
Accessing SQLite from software
The most common way to interact with an SQLite (or other) database is through function calls to open a connection to the database; execute queries that can read, insert or update existing data; and close the connection to the SQLite database so that any changes are flushed to the SQLite file. In the simple example below, we suppose that the database has already been created with a table called employee that has two columns: the string name and the integer age.
#!/usr/bin/env python3
# For Python we can use the module sqlite3, installed in a virtual environment,
# to access an SQLite database
import sqlite3
age = 34
# Connect to the database...
dbase = sqlite3.connect("foo.sqlite")
dbase.execute("INSERT INTO employee(name,age) VALUES(\"John Smith\"," + str(age) + ");")
# Close the database connection
dbase.close()
# Using R, the first step is to install the RSQLite package in your R environment,
# after which you can use code like the following to interact with the SQLite database
library(DBI)
age <- 34
# Connect to the database...
dbase <- dbConnect(RSQLite::SQLite(),"foo.sqlite")
# A parameterized query
query <- paste(c("INSERT INTO employee(name,age) VALUES(\"John Smith\",",toString(age),");"),collapse='')
dbExecute(dbase,query)
# Close the database connection
dbDisconnect(dbase)
#include <iostream>
#include <string>
#include <sqlite3.h>
int main(int argc,char** argv)
{
int age = 34;
std::string query;
sqlite3* dbase;
sqlite3_open("foo.sqlite",&dbase);
query = "INSERT INTO employee(name,age) VALUES(\"John Smith\"," + std::to_string(age) + ");";
sqlite3_exec(dbase,query.c_str(),nullptr,nullptr,nullptr);
sqlite3_close(dbase);
return 0;
}
Limitations
As its name suggests, SQLite is easy to use and intended for relatively simple databases which are neither excessively large (hundreds of gigabytes or more) nor too complicated in terms of their entity-relationship diagram. As your SQLite database grows in size and complexity, the performance could start to degrade, in which case the time may have come to consider the use of more sophisticated database software which uses a client-server model. The SQLite web site includes an excellent page on Appropriate Uses For SQLite, including a checklist for choosing between SQLite and client-server databases.