SQLite Database
This is a simple wrapper for Python’s built-in sqlite package. It uses simple API to interact with the database in a NoSQL-like fashion.
Table of content.
In Depth of Available Features
Note: To test these features, make sure you copy and paste this snippet:
from sqlite_database import op
from sqlite_database.operators import eq, like, between
value = 0 # You can use almost anything.
For table and database, you can create any dummy database, but here’s one (assuming you have pasted the above snippet):
from sqlite_database import Database, integer, text
from sqlite_database import this
db = Database(":memory:")
my_table = db.create_table('my_table', [
integer('row'),
text('row2')
])
Available Filters
this
:
data = table.select({
"row": this == value
})
Key-Value:
data = table.select({
'row': value
})
List of functions:
data = table.select([
eq('row', value)
])
Any other variations are just math operators. For the list, operations can be obtained from the operators
module.
Available Constraints
Like constraint:
data = table.select([
like('row2', 'te%t')
])
Between constraints:
data = table.select([
between('row', 0, 10)
])
Export
You can export the database/table to CSV. For now, import functionality will not be added.
Make sure to add this line
from sqlite_database.csv import to_csv_file, to_csv_string
You can use to_csv_string
rather than to_csv_file
, all you need is to pass the table or database.
Note: The return type for the database is a tuple indicating (name, csv)
, and by that, to_csv_file
would make sure that filename passed is a directory and the content will be all table files. E.g: /path/database/table.csv
Export Table
# To String
csv = to_csv_string(db.table("table"))
# To file
to_csv_file(db.table("table"), "table.csv")
Export Database
# To String
csv = to_csv_string(db) # list[(name, csv)]
# To directory
to_csv_file(db.table("table"), "DatabasePath")